Skip to main content
Tutorial Free

Moving Controllers to sub-folders in a correct way

October 19, 2015
1 min read
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/Admin and 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:
namespace App\Http\Controllers\Admin;
2. Use Controller - from your inner-namespace Laravel won't "understand" extends Controller, so you need to add this:
use App\Http\Controllers\Controller;
3. Routes - specify full path This wouldn't work anymore:
Route::get('admin', 'AdminController@getHome');
This is the correct way:
Route::get('admin', 'Admin\AdminController@getHome');
And that's it - now you can use your controller from sub-folder.

Enjoyed This Tutorial?

Get access to all premium tutorials, video and text courses, and exclusive Laravel resources. Join our community of 10,000+ developers.

Comments & Discussion

No comments yet…

We'd Love Your Feedback

Tell us what you like or what we can improve

Feel free to share anything you like or dislike about this page or the platform in general.