0
votes

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?

3

3 Answers

3
votes

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
2
votes

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.

0
votes

on your web.php

Route::prefix('/admin')->name('admin.')->namespace('admin')->group(function()
{ 
   //routes prefix with admin
     Route::get('/dashboard', 'AdminController@dashboard')->name('admindashboard');
   //its behave like
    Route::get('admin/dashboard', 'AdminController@dashboard')->name('admin.admindashboard')
});

Now call it on blade file

{{ route('admin.admindashboard') }}