欢迎各位兄弟 发布技术文章

这里的技术是共享的

You are here

laravel different routes for different roles 不同的角色 不同的 url 路由 有大用

If you want to accomplish that using middleware you need to do following -

  1. Create two middlewares, one for admin and one for simple user.

  2. Then create two route group in your routes file i.e. routes/web.php

  3. Protect one route group with admin middleware, and put all of your admin related routes in that group. Protect another route group with simple-user middleware and put all of your admin related routes in that group.

Route::group(['middleware' => ['auth', 'admin']], function() {
    // put all your admin routes here
});

Route::group(['middleware' => ['auth', 'simple-user']], function() {
    // put all your simple user routes here
});

来自  https://stackoverflow.com/questions/41413598/laravel-5-3-protecting-routes-from-user-have-different-...



Route::get('appointment', function()
{
    if (auth()->user()->isDoctor())
    {
        app()->call('Doctor\AppointmentController@index')
    }
    elseif(auth()->user()->isPatient())
    {
        app()->call('Patient\AppointmentController@index')
    }
});

来自  https://stackoverflow.com/questions/40789020/using-the-same-route-for-different-types-of-users-in-la...



Route::get('/', function () {
    if (auth()->check()) {
        if (auth()->user()->isAdmin()) {
            return redirect()->route('');
        } elseif (auth()->user()->isUser()) {
            return redirect()->route();
        } else {
            return view('index');
        }
    }
    return redirect()->to('login');
});


public function getIndex()
{
    if(Auth::user()->isAdmin()) {
        //Admin
        return $this->getAdminIndex();
    } else {
        //No admin
        return $this->getUserIndex();
    }
}

protected function getAdminIndex()
{
    return view('admin.index');
}

protected function getUserIndex()
{
    return view('user.index');
}



来自 https://stackoverflow.com/questions/43831104/laravel-one-route-with-multiple-controllers-based-on-user-role


普通分类: