Laravel 5 multiple dashboard routes for admins and managers

(I asked this question before, and found a way that worked for a while, but soon got out of hand. So now I'm looking for the perfect solution to achieve this Laravel path.)

The problem is quite simple, when the admin goes to http://example.com/dashboard , they should see the admin dashboard, and when the manager goes to the same link, they should see the managers dashboard.

Whereas in the past I used a call pagecontroller

, and then depending on the role of the user, I called the corresponding controller of the admin or manager control panel.

    // ROUTES.php
    Route::group(['middleware' => 'auth'], function () {
        Route::get('dashboard', 'PagesController@dashboard');
        Route::get('users', 'PagesController@manageUsers');
    });

    // PagesController
        public function dashboard(){

           if($this->user->isAdmin()){
                return $controller = app()->make('App\Http\Controllers\Admin\dashboard')->index();
            }
           if($this->user->isManager()){
                   return $controller = app()->make('App\Http\Controllers\Manager\dashboard')->index();
                }
        }

      

Now the problem with this approach is that I can no longer call the middleware on the dashboard controller because the process doesn't involve calling the kernel.

I am sure there must be a way to achieve such a basic function, I would be very grateful if someone can figure it out.

+3


source to share


1 answer


I think the reason is that this is not a default Laravel feature because it is inconsistent with what routes are supposed to represent. In other words, this is not the Laravel Way. Why not redirect the user based on their role?



// ROUTES.php
Route::group(['middleware' => 'auth'], function () {
    Route::get('dashboard', function() {
        if($this->user->isAdmin())
            return redirect('/dashboard/admin');
        if($this->user->isManager())
            return redirect('/dashboard/manager');

        return redirect('/home');
    });

    Route::get('dashboard/admin', 'Admin\dashboard@index');
    Route::get('dashboard/manage', 'Manager\dashboard@index');
    Route::get('users', 'PagesController@manageUsers');
});

      

+2


source







All Articles