I try to use Silex Framework, but i have a problem with namespaces I think.
when I instanciate my class MainController i have the following error : Class "MainController" does not exist
here the namespace declaration in my MainController.php :
namespace App\Controllers;
use Silex\Application;
class MainController implements \Silex\ControllerProviderInterface {
....
in my app.php :
$app->mount("/", new \App\Controllers\MainController());
And i've an autoload in my composer.json :
"autoload": {
"psr-4": {"App\\": "app/"}
}
scruture of my project is like it :
|--app/
|----app.php
|----controllers/
|-------MainController.php
|--web/
|----index.php
Thanks a lot for your help :)
I believe your problem is caused by the way you named your directory
controllers
. According to the documentation aboutPSR-4
standard:So, rename your directory to
Controllers
and re-runcomposer update
.Also, take a look at ServiceControllerProvider about the proper way of setting controller instance as a callback. Passing new instance might not be the best (if not wrong) way of doing things. You should be doing something like: