Moving Controllers to sub-folders in a correct way - Laravel Daily

来源:转载

If your application gets bigger, it makes sense to structure Controllers with sub-folders. But it takes a little more effort than just moving the files here and there. Let me explain the structure.

For example, we want to have a sub-folder app/Http/Controllers/Adminand then inside of it we have our AdminController.php, that’s fine. What we need to do inside of the file itself:

1. Correct namespace– specify the inner folder:

PHP

namespace App/Http/Controllers/Admin;

2. Use Controller– from your inner-namespace Laravel won’t “understand” extends Controller, so you need to add this:

PHP

use App/Http/Controllers/Controller;

3. Routes – specify full path

This wouldn’t work anymore:

PHP

Route::get('admin', '[email protected]');

This is the correct way:

PHP

Route::get('admin', 'Admin/[email protected]');

And that’s it – now you can use your controller from sub-folder.

Facebook Twitter Google+ LinkedIn

分享给朋友:
您可能感兴趣的文章:
随机阅读: