In the previous chapter we explained how to create a backend folder in the Laravel framework.
Now let’s add another piece of content. It’s about automatic loading.
There is also a start directory in our app directory. It says this:
In addition to using Composer, you may use the Laravel class loader to
load your controllers and models. This is useful for keeping all of
your classes in the "global" namespace without Composer updating.
Let me translate: In addition to using Composer, you can also use Laravel's class loader to load your controllers and models.
This is useful for keeping your classes in the global namespace without using Composer updating.
We have a code fragment here
ClassLoader::addDirectories(array(
app_path().'/commands',
app_path().'/controllers',
//app_path().'/controllers/admin',
app_path().'/models',
app_path().'/database/seeds',
));
Yes, very familiar. These folders are folders, controllers, commands, models, and database seeds that we often use
The contents in these folders can be automatically loaded into the global namespace.
But there is one more content that I commented. What is the purpose of this content...
means that the controller in the admin in our controllers directory also has a global namespace. In this way, we can directly pass
in the routing tableOur controller name is here to receive.
I tested it and it was indeed successful, and composer dumpautoload is no longer needed
But there is still a problem. What should I do if there is a controller in the admin folder with the same name as the one outside?
I also did an experiment, and finally found that it will only choose one of them. As for which one to choose, it depends on the loading order above.
If yes
app_path().'/controllers',
app_path().'/controllers/admin',
Then the external controller is valid, otherwise, the controller in the admin folder is valid.
Combined with what I have written before. In fact, the safest and most advanced method is to use namespaces.
But it will be a little more complicated to write... You can consider our method for small projects. At the worst, just pay attention and don't let these controllers conflict.
It’s not like you can’t control it, right.
Do one thing at a time, and do well.
Best Wishes.