Ad

Laravel Admin/user Route Prefix Approach

- 1 answer

so I have to make this system for transport management. The user can log in create/update/edit all his trips. But the admin can do the same for all users. I have divided user and admin in to route prefixes:

Route::group(['prefix' => 'admin/', 'middleware' => ['auth','admin']], function(){
Route::resource('trips', 'TripsController',
        array('except' => array('show')));}

Route::group(['prefix' => 'user/', 'middleware' => ['auth', 'user']], function(){

Route::resource('trips', 'TripsController',
        array('except' => array('show')));
}

The problem is in every method of the TripController I have to pass route variable with the correct url (the admin request will have a 'admin' prefix, and the users will have 'user' prefix)

return View('trips.index', compact('users', 'route'));

The question is there a way to do this nicely or should I just pull the trips Route::resource out of the both groups so that it wouldn't have any groups? What is the correct approach here?

Ad

Answer

I use this approach:

Route::group(['namespace' => 'Admin', 'as' => 'admin::', 'prefix' => 'admin'], function() {

    // For Other middlewares

    Route::group(['middleware' => 'IsNotAuthenticated'], function(){

        // "admin::login"
        // http://localhost:8000/admin/login
        Route::get('login', ['as' => 'login', 'uses' => '[email protected]']);

    });


    // For admin middlewares
    Route::group(['middleware' => 'admin'], function(){

        // "admin::admin.area.index"
        // http://localhost:8000/admin/area/{area}
        Route::resource('Area', 'AreaController');

        // "admin::dashboard"       
        // http://localhost:8000/admin/
        Route::get('/', ['as' => 'dashboard', 'uses' => '[email protected]']);

    });

});

Whenever I need to access url in blade templates I simply use route helper method.

// For resourceful routes    

{{ route('admin::admin.city.index') }}

or

//For regular get/post routes 

{{ route('admin::dashboard') }}

Or simply run artisan command to list route names.

php artisan route:list
Ad
source: stackoverflow.com
Ad