jsonDoge
jsonDoge

Reputation: 722

Laravel admin/user route prefix approach

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?

Upvotes: 0

Views: 11576

Answers (2)

Erich García
Erich García

Reputation: 1874

I did it with this:

//Admin Group&NameSpace
Route::namespace('Admin')->prefix('admin')->group(function () {
    Route::get('/dashboard', 'DashboardController@index')->name('dashboard')->middleware('auth');
});

Even you can customize the ->middleware('auth'); with a custom middleware role based.

Upvotes: 2

Rajender Joshi
Rajender Joshi

Reputation: 4205

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' => 'AdminController@index']);

    });


    // 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' => 'AdminController@dashboard']);

    });

});

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

Upvotes: 3

Related Questions