0

I am having trouble routing with controllers in subfolders. I have tried the solution proposed in Laravel Controller Subfolder routing, but I can't get it to work.

Folder structure

 HTTP
   Controllers
      Admin
        AdminControllers
      User
        UserControllers
      BaseController

Admincontrollers are defined in the 'App\HTTP\Controllers\Admin' namespace

Routes file

Route::group(['middleware'=> 'admin','prefix' => 'admin'], function() {
  Route::get('home', 'AdminHomeController@index');
  Route::get('home', 'Admin\AdminHomeController@index');
  Route::resource('events', 'AdminEventController');
  Route::resource('events', 'Admin\AdminEventController');
  Route::get('myevents', 'AdminEventController@myevents');
  Route::get('myevents', 'Admin\AdminEventController@myevents');
  Route::resource('groups', 'AdminGroupController');
  Route::resource('users', 'AdminUserController');
});

This does seem weird, but it is the only way to keep it working right now. If I delete

  Route::get('myevents', 'Admin\AdminEventController@myevents');
  //errormessage Class App\Http\Controllers\AdminEventController does not exist

If I delete

  Route::get('myevents', 'AdminEventController@myevents');
 //errormessage Action App\Http\Controllers\AdminEventController@myevents not defined. 

If I put the controllers in the controller namespace (not the admin one) I still get

  //errormessage Class App\Http\Controllers\AdminEventController does not exist

When the only route added is

  Route::resource('events', 'AdminEventController');
Community
  • 1
  • 1
JorenV
  • 352
  • 3
  • 16
  • 1
    Have you tried the fully-qualified controller name? eg. `Route::get('home', 'App\Http\Controllers\Admin\AdminHomeController@index');` My guess is that Laravel (or maybe the composer autoloader) is trying to guess what you're referring to and getting it wrong. – Kryten Jun 01 '15 at 19:59
  • That did not the trick, but the new error gave me an idea and it's fixed now – JorenV Jun 01 '15 at 20:11

2 Answers2

0

The problem were the calls in the views:

changing

<td>{!!Html::linkAction('AdminEventController@show', $event->name, $event->slug)!!}</td>

to

<td>{!!Html::linkAction('Admin\AdminEventController@show', $event->name, $event->slug)!!}</td>

fixed it.

The Laravel 5 solution in Laravel Controller Subfolder routing is correct. The problem was not in the routes file or controllers.

Community
  • 1
  • 1
JorenV
  • 352
  • 3
  • 16
0

Yes If your application gets bigger like this, 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:

Correct namespace – specify the inner folder:

namespace App\Http\Controllers\Admin;

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

use App\Http\Controllers\Controller;

Routes – specify full path This wouldn’t work anymore:

Route::get('admin', 'AdminController@index');

This is the correct way:

Route::get('admin', 'Admin\AdminController@index');

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

Reference (Tested): http://laraveldaily.com/moving-controllers-to-sub-folders-in-a-correct-way/ By: Povilas Korop

Ramin
  • 19
  • 6