Support the ongoing development of Laravel.io →
Configuration Architecture
Last updated 2 years ago.
0

Namespaces are used to allow classes to have the same name to co-exist within the same application. Namespacing should follow https://github.com/php-fig/fig-standards/blob/master/accepted/PSR-0.md namespacing standards.

They can be a pain, so I would not recommend using namespaces unless you really need to.

If you want them in multiple folders, it'd be beter to use Composer's auotloading over namespaces: https://getcomposer.org/doc/01-basic-usage.md#autoloading

Last updated 2 years ago.
0

hi bukhl, thank you for your answer, let's say that i have admin section on site and i want to store controllers for admin section in app/controllers/admin should i go then with your solution and composer autoloader or i should do it on some other way. when i was working with CI i used to have separate folders for separate sections on the site. How should this be handled in Laravel. thanks

Last updated 2 years ago.
0

The situation you mentioned is indeed a good example of why to use namespacing for your controllers.
You can then have controllers with the same classname as long as they are in a different namespace.

Because the controllers folder is under the classmap section of the composer autoloader it should be able to load your controllers even without the namespaces but you'll need to run composer dump-autoload each time you add a new class.

I actually recommend namespacing all your controllers and models.
I would find it a bit confusing to namespace some of them but not all of them.

If you add the namespace under a psr-4 section in your composer.json you wont need to regenerate the autoloader each time you add a new controller while in development. Though I still recommend leaving the classmap as it should be faster in production.

Last updated 2 years ago.
0

Sign in to participate in this thread!

Eventy

Your banner here too?

Moderators

We'd like to thank these amazing companies for supporting us

Your logo here?

Laravel.io

The Laravel portal for problem solving, knowledge sharing and community building.

© 2024 Laravel.io - All rights reserved.