如何使用laravel管理中间件以在自己的仪表板中重定向不同的用户?

时间:2017-03-23 08:03:27

标签: php laravel

我试图在什么类型的用户登录其重定向到自己的仪表板时这样做。登录时用户和SuperAdmin是可以的,但是当我使用branchAdmin帐户登录时,它不会重定向到分支仪表板,它会重定向到用户仪表板。并使用branchAdmin包含。我所做的。

路线:

    Route::group(['middleware'  =>  [ 'auth', 'isAdmin']], function(){
        Route::get('/profile','ProfileController@getIndex');
    });

    Route::group(['middleware'  =>  [ 'auth', 'isBranchAdmin']], function(){
        Route::get('/branch','BranchController@gettIndex');
    });

    Route::group(['middleware'  =>  [ 'auth', 'isNotAdmin']], function(){
    Route::get('/Super/admin', 'AdminController@getIndex');
        });

查看:

<div class="col-xs-12 col-sm-12 col-md-3 col-lg-3">
    @if(Auth::check() && Auth::user()->type === 'User')
        <ul class="nav nav-pills nav-stacked">
            <li role="presentation" class="active">
                <a id="bootstrap-overrides" href="/home">
                    Home
                </a>
            </li>
            <li role="presentation">
                <a id="bootstrap-overrides" href="/contact">
                    Contact
                </a>
            </li>
            <li role="presentation">
                <a id="bootstrap-overrides" href="/about">
                    About
                </a>
            </li>
            <li role="presentation">
                <a id="bootstrap-overrides" href="/blog">
                    Blog
                </a>
            </li>
            <li role="presentation">
                <a id="bootstrap-overrides" href="/faqs">
                    FAQs
                </a>
            </li>
        </ul>
    @elseif(Auth::check() && Auth::user()->type === 'Admin')
        <ul class="nav nav-pills nav-stacked">
            <li role="presentation" @if(Request::path() === 'companies') class="active" @endif>
                <a href="/companies">
                    Companies
                </a>
            </li>
            <li role="presentation" @if(Request::path() === 'branchies') class="active" @endif>
                <a href="/branchies">
                    Branchies
                </a>
            </li>
        </ul>
    @elseif(Auth::check() && Auth::user()->type === 'BranchAdmin')
        <ul class="nav nav-pills nav-stacked">
            <li role="presentation" @if(Request::path() === 'medicines') class="active" @endif>
                <a href="/medicines">
                    Medicines
                </a>
            </li>
            <li role="presentation" @if(Request::path() === 'stock') class="active" @endif>
                <a href="/stock">
                    Stock_details
                </a>
            </li>
        </ul>
    @endif
</div>

中间件:

BranchAdmin

class BranchAdmin
{
    public function handle($request, Closure $next){
        if(Auth::user()->type === 'BranchAdmin'){
            return redirect('/branch');
        }
        return $next($request);
    }
}

UserIsAdmin:

class UserIsAdmin
{
    public function handle($request, Closure $next)
    {
        if(Auth::user()->type === 'Admin'){
            return redirect('/Super/admin');
        }
        return $next($request);
    }
}

UserIsNotAdmin:

class UserIsNotAdmin
{
    public function handle($request, Closure $next)
    {
        if(Auth::user()->type === 'User'){
            return redirect('/profile');
        }
        return $next($request);
    }
}

1 个答案:

答案 0 :(得分:1)

Laravel 5的解决方案。*转到登录控制器:

app\Http\Controllers\Auth\LoginController.php

查找和编辑方法:

protected function authenticated(Request $request, $user)
  {
    // Do something with user
    // redirect user to dashboard
  }