Silex namespace : class MainController does not exist

692 views Asked by At

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 :)

1

There are 1 answers

5
Artamiel On BEST ANSWER

I believe your problem is caused by the way you named your directory controllers. According to the documentation about PSR-4 standard:

5) Alphabetic characters in the fully qualified class name MAY be any combination of lower case and upper case.

6) All class names MUST be referenced in a case-sensitive fashion.

So, rename your directory to Controllers and re-run composer 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:

$app->get('/', 'App\\Controllers\\MainController::index');