Silex namespace: MainController class does not exist
I am trying to use Silex Framework but I have a problem with namespaces I think.
when i initialize my MainController class i have the following error: class "MainController" does not exist
here's 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 have autoloading in my composer.json:
"autoload": {
"psr-4": {"App\\": "app/"}
}
scruture of my project is like this:
| --app /
| ---- app.php
| ---- Controllers /
| ------- MainController.php
| --web /
| ---- index.php
Many thanks for your help:)
source to share
I believe your problem is caused by the way you named your directory controllers
. According to the documentation about the standard PSR-4
:
5) The alphabetic characters in the full class name MAY be any combination of lower case and upper case.
6) All class names MUST be case-sensitive.
So, rename the directory to controllers
and run composer update
.
Also, take a look at ServiceControllerProvider for the correct way to set up a controller instance as a callback. Passing a new instance might not be the best (if not wrong) way to do things. You should be doing something like:
$app->get('/', 'App\\Controllers\\MainController::index');
source to share