Kenny Yap
Kenny Yap

Reputation: 1337

Proper way to create controllers under a subfolder in laravel 5

I have been struggling with this issue for the pas hour and I'm not sure what have I done wrong. So the case is like this. I wanted to create a controller folder go group different controllers into their groups. By default laravel projects created a controller folder structure like this

Http
--Controller
----Auth

So what I would like to do is to make something like this

Http
--Controller
----Auth
----Folder_a
----Folder_b
----Folder_c

After making my folders, the controllers in my folders are also properly namespaced like so

<?php namespace App\Http\Controllers\Folder_a;

    /*
    |--------------------------------------------------------------------------
    | Use the main controller to allow extend to the main controller
    |--------------------------------------------------------------------------

    */

    use App\Http\Controllers\Controller;

class SomethingController extends Controller {
        /* Do something here*/
}

And finally in my routes.php i call the actions like such

Route::get('/action1/', array('as' => 'action1', 'uses' => 'SomethingController@action1'));

But some how when i try to navigate to that site it gives me this error

ReflectionException in compiled.php line 1029:
Class App\Http\Controllers\SomethingController does not exist

Noticed that it still go into the default folder App\Http\Controllers\ to find the controller but if I do like this

Route::get('/action1/', array('as' => 'action1', 'uses' => 'Folder_a\SomethingController@action1'));

Then everything will be fine... What have I done wrong in this case? also i have tried composer dump-autoload, nothing has changed.

Upvotes: 5

Views: 1751

Answers (3)

Francesco Taioli
Francesco Taioli

Reputation: 2876

Other answer are right, but if you want to delete the folder name from the web.php, so it become

Route::get('/action1/', array('as' => 'action1', 'uses' => 'SomethingController@action1'));
  1. In your controller delete Folder_a from namespace, so it become

    namespace App\Http\Controllers;

  2. composer dump-autoload

Upvotes: 0

Davor Minchorov
Davor Minchorov

Reputation: 2076

I don't think there's any need to group controllers in folders. You'll mostly have 1 controller for every resource and every resource is its own thing. There's nothing wrong if you have 50-100 controllers. If you put them in folders, you'll end up with that many folders, so nesting the files won't help.

Start normally and then refactor if you really need to.

Upvotes: 0

lukasgeiter
lukasgeiter

Reputation: 153150

You have done nothing wrong. This is the expected behavior! Laravel will search for the specified controller relative to App\Http\Controllers by default. So you have to specify the namespace from there. If you have many routes that lead to a controller in Folder_a you can use a route group to clean things up:

Route::group(['namespace' => 'Folder_a'], function(){
    Route::get('/action1/', array('as' => 'action1', 'uses' => 'SomethingController@action1'));
    // more routes
});

Upvotes: 7

Related Questions