Zach C. Zach C. - 3 months ago 39
Twig Question

php namespace class not found - twig composer

I searched and tried all the answers and have had no success.

I have a small MVC that was working just fine till I added namespaces. Errors everywhere. My current error I Think might be related to composer autoload. As my current error is with Twig. Which I should note works perfectly fine when I remove all namespaces.

My Last error was the same error as this but with View class. I resolved it by putting View.php in Viewer folder instead of either the Core or Controller folder

Error

Fatal error: Uncaught Error: Class 'app\Core\Viewer\Twig_Loader_Filesystem' not found in G:\site\app\Core\Viewer\View.php:40 Stack trace: #0 G:\site\app\Core\Controller\Controller.php(13): app\Core\Viewer\View->__construct('index.twig', Array) #1 G:\site\app\controllers\mycms.php(14): app\Core\Controller\Controller::view('index.twig', Array) #2 [internal function]: myCMS::index() #3 G:\site\app\Core\App.php(68): call_user_func_array(Array, Array) #4 G:\site\public\index.php(13): Core\App->__construct() #5 {main} thrown in G:\site\app\Core\Viewer\View.php on line 40


File Structure - Not full structure only effected files

| common.php - Includes all dependency's
| composer.json
|
├────app
| | bootstrap.php - simply includes everything in app
| |
| ├───Core
| | App.php - uses namespace app\Core
| | Class name App
| |
| ├───Controller
| | Controller.php - uses namespace app\Core\Controller
| | Class name Controller
| ├───Viewer
| | Viewer.php - uses namespace app\Core\Viewer
| | Class name View
|
├───public
| | index.php - includes common.php which includes everything else
|
├───vendor - typical composer structure
| | autoload.php
├───twig


composer.json - I feel this is where the problem is. I used "composer dumpautoload" already

{
"require": {
"twig/twig": "~1.0",
other dependency's...
},
"autoload": {
"psr-4": {
"app\\Core\\": "app/Core"
}
}
}


Controller.php


namespace app\Core\Controller;

use app\Core\Viewer as View;

class Controller {

public static function view($viewName, $data)
{
$view = new View($viewName, $data);

...
}
}


View.php

namespace app\Core\Viewer;

class View
{
....
}


That should cover what I think the problem might be. Let me know if you want more posted and I will update my post.

Answer

I think that in your View class you are using the Twig_Loader_Filesystem right?

So you have to use it like \Twig_Loader_Filesystem notice the leading backslash.