Laravel organization and startup subdirectories

I want to structure my laravel application so that all my code is in the src directory. My project structure will look something like this. How can I do this, where can I still call Route::get('accounting/item/{id}',' AccountingItemController@getId ')

I want to avoid adding each module under src to ClassLoader. Is there any way to tell the classloader to load all subdirectories in the src parent directory?

 app app/src app/src/accounting app/src/accounting/controllers app/src/accounting/models app/src/accounting/repos app/src/accounting/interfaces app/src/job app/src/job/controllers app/src/job/models app/src/job/repos app/src/job/interfaces 
+4
source share
2 answers

Yes, it's called PSR-0.

You must write all your code. Usually you will have the name of the provider in which you use the top-level namespace. Your application structure should look something like this.

 app/src/Vendor/Accounting/Controllers app/src/Vendor/Job/Controllers 

Then your controllers will be replaced accordingly.

 namespace Vendor\Accounting\Controllers; 

And when using them in routes.

 Route::get('accounting/item/{id}','Vendor\Accounting\Controllers\ ItemController@getId '); 

Finally, you can register your namespace with Composer in composer.json .

 "autoload": { "psr-0": { "Vendor": "app/src" } } 

Of course, if you do not want the Vendor top-level namespace to be removed, but you need to register each component as PSR-0.

 "autoload": { "psr-0": { "Accounting": "app/src", "Job": "app/src", } } 

After execution, run composer dump-autoload once, and you can add new controllers, models, libraries, etc. Just make sure the directory structure matches the namespace of each file.

+11
source

Do you have a composer installed? You should use this:

composer dump-autoload

But you can add directories to the Laravel class loader. Check out the link here: http://laravel.com/api/class-Illuminate.Support.ClassLoader.html

+1
source

Source: https://habr.com/ru/post/1488987/


All Articles