rotexsoft / slim-skeleton-mvc-app

This is a template web-application (powered by SlimPHP), that can be extended to build more complex web applications.
BSD 3-Clause "New" or "Revised" License
38 stars 8 forks source link

Class autoloading #1

Closed Ziggizag closed 7 years ago

Ziggizag commented 8 years ago

Hi,

I see Hello controller is only available as it is declared in Composer:

1) In autoload_classmap.php: 'Hello' => $baseDir . '/src/controllers/Hello.php'

2) 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?

Ziggizag commented 8 years ago

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.

rotexdegba commented 8 years ago

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.

rotexdegba commented 7 years ago

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.