Class autoloading
Ziggizag opened this issue · 3 comments
Hi,
I see Hello controller is only available as it is declared in Composer:
-
In autoload_classmap.php:
'Hello' => $baseDir . '/src/controllers/Hello.php'
-
In autoload_static.php:
'Hello' => __DIR__ . '/../..' . '/src/controllers/Hello.php'
But what about other - user defined - models and controllers in the application?
What is the proper way to declare them as it seems the app itself does not look for them by itself?
Yeap, I've already got it:
The best way IMHO is to modify composer.json
so it looks like:
"autoload": {
"classmap": [ "src/", "tests/", "vendor/slim/" ],
"psr-4": {
"app\\controllers\\": "src/controllers",
"app\\models\\": "src/models"
}
}
and then to modify namespaces_for_controllers
within dependencies.php
:
$container['namespaces_for_controllers'] = ['\\Slim3MvcTools\\Controllers\\', '\\app\\controllers\\'];
After executing composer update
one only needs to namespace custom controllers and models appropriately.
Thanks for pointing this out. I normally use the composer dumpautoload
command anytime I add a new class file to my projects. I should have added the psr-4
autoload option in the composer.json file. I will open an issue to do this.
By the way I will resume active development on this project in July.
I have decided not to include a PSR-4 entry in the autoload section of the composer.json. I will leave this to users to define, since I can't know ahead of time which namespace(s) the user's application's controllers will be in.