0

我正在尝试使用 'auth' 和 'auth.admin' 中间件过滤路由,这应该类似于 laravel 4.2 的 Route::filter。但它不起作用。这是我的路线

Route::group(['prefix' => 'admin', 'middleware' => ['auth', 'auth.admin']], function()
{
   // ... 
});

内核.php

protected $routeMiddleware = [
    'auth' => \App\Http\Middleware\Authenticate::class,
    'auth.basic' => \Illuminate\Auth\Middleware\AuthenticateWithBasicAuth::class,
    'guest' => \App\Http\Middleware\RedirectIfAuthenticated::class,
    'auth.admin' => \App\Http\Middleware\RedirectIfAdmin::class,
    'role' => Zizaco\Entrust\Middleware\EntrustRole::class,
    'permission' => Zizaco\Entrust\Middleware\EntrustPermission::class,
    'ability' => Zizaco\Entrust\Middleware\EntrustAbility::class,
];

重定向IfAdmin.php

        <?php

        namespace App\Http\Middleware;

        use Closure;
        use Entrust;
        class RedirectIfAdmin
        {
            /**
             * Handle an incoming request.
             *
             * @param  \Illuminate\Http\Request  $request
             * @param  \Closure  $next
             * @return mixed
             */
            public function handle($request, Closure $next)
            {
                if (!Entrust::hasRole(config('customConfig.roles.admin'))) {
                    return redirect()->route('dashboard')
                                ->with('error', 'Access Denied');
                }
                return $next($request);
            }
        }
4

1 回答 1

0

正如你所说,你的dashboard路由是针对经过身份验证的用户的,但是你检查用户是否不在admin role发送到仪表板,当他被发送到仪表板时,他被重定向回来,可能是由于另一个中间件启动,然后发送回登录和从再次登录到仪表板,所以只需!从你的 if 条件中删除。

于 2016-05-13T11:29:29.763 回答