如何使用laravel管理中间件?

时间:2017-04-06 12:57:46

标签: php laravel

我创建了3个用户('超级管理员','分支管理员''用户')。我试图这样做的时候'超级管理员'是登录它没有去其他2个用户仪表板用户('分支管理员,用户')。但它显示了一个页面,其中有太多的重定向"当我在浏览器中提供任何其他用户的URL时,它会从其自己的仪表板重定向其仪表板。和其他2个用户一样????

路线:

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

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

    Route::group(['middleware'  =>  [ 'auth', 'isAdmin']], 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/'.Auth::user()->branch->id);
        }
        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)

您的中间件逻辑似乎不正确。我认为你肯定会有太多的重定向因为这个。我以一个中间件为例。

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

你在说什么

If the user is of type 'User', always redirect them to '/profile'

因此,如果“用户”类型的用户转到http://website/profile,则会一遍又一遍地将其重定向到配置文件。

你应该做的是实际做中间件的用途:阻止入侵者:)。例如:在UserIsNotAdmin中间件中,执行此操作

if( !Auth::user()->type === 'User' ){
    redirect('/home');
}
return $next($request);

转换为

if the user IS NOT of type 'User', send them home. Else, let them in.