Home  >  Article  >  Backend Development  >  php Laravel Framework - Creating a Backend Folder 2_PHP Tutorial

php Laravel Framework - Creating a Backend Folder 2_PHP Tutorial

WBOY
WBOYOriginal
2016-07-13 10:28:451326browse

In the previous chapter we explained how to create a backend folder in the Laravel framework.

php Laravel framework to create a backend folder


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 table

Our 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.


www.bkjia.comtruehttp: //www.bkjia.com/PHPjc/780966.htmlTechArticleIn the previous chapter we explained how to create a backend folder in the Laravel framework. php Laravel framework to create a backend Folder Now let's add another piece of content. It's about automatic loading...
Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn