Laravel Middleware永远重定向

时间:2015-02-26 01:40:16

标签: laravel laravel-5 laravel-middleware

有人可以帮我中间件吗?我正在获得无休止的重定向,这让我疯狂了一个星期。

/****** Route **********/
Route::group(['middleware' => 'auth'], function() {
    Route::get('/profile', 'UserController@profile');

    // Superadmin & Admin
    Route::group(['middleware' => 'admin'], function() {
        Route::get('/dashboard', 'UserController@dashboard');
    });

    // Superadmin
    Route::group(['middleware' => 'superadmin'], function() {
            Route::get('/settings', 'UserController@settings');
    });
});

/****** Middleware *******/
// admin
public function handle($request, Closure $next)
{
    if (Auth::check()) {
        if (User::isAdmin()) { // validate if superadmin or admin
        return $next($request);
    } else {
        return redirect('/profile');
    } 
}

// superadmin
public function handle($request, Closure $next)
{
    if (Auth::check()) {
    if (User::isSuperAdmin()) { // validate if superadmin
        return $next($request);
    } else {
        return redirect('/profile');
    }
    }
}

好的只是再次更新了代码但仍有问题 永远重定向任何想法?

// SuperAdmin
public function handle($request, Closure $next)
    {
        if (Auth::check()) {
            if (!User::isSuperAdmin()) {
                return new RedirectResponse(url('/profile'));
            }
        }

        return $next($request);
    }

// admin and superadmin
public function handle($request, Closure $next)
    {
        if (Auth::check()) {
            if (!User::isAdmin()) {
                return new RedirectResponse(url('/profile'));
            }
        } 

        return $next($request);
    }

// auth
public function handle($request, Closure $next)
    {
        if ($this->auth->guest())
        {
            if ($request->ajax())
            {
                return response('Unauthorized.', 401);
            }
            else
            {
                return redirect()->guest('/login');
            }
        }
    return $next($request);
}

1 个答案:

答案 0 :(得分:1)

修正了它。原因是因为$ middleeware和$ routeMiddleware都使用相同的中间件

protected $middleware = [
        'Illuminate\Foundation\Http\Middleware\CheckForMaintenanceMode',
        'Illuminate\Cookie\Middleware\EncryptCookies',
        'Illuminate\Cookie\Middleware\AddQueuedCookiesToResponse',
        'Illuminate\Session\Middleware\StartSession',
        'Illuminate\View\Middleware\ShareErrorsFromSession',
        'App\Http\Middleware\VerifyCsrfToken',
        //'App\Http\Middleware\SuperAdminMiddleware',
        //'App\Http\Middleware\AdminMiddleware',
    ];

    /**
     * The application's route middleware.
     *
     * @var array
     */
    protected $routeMiddleware = [
        'auth'          => 'App\Http\Middleware\Authenticate',
        'auth.basic'    => 'Illuminate\Auth\Middleware\AuthenticateWithBasicAuth',
        'guest'         => 'App\Http\Middleware\RedirectIfAuthenticated',
        'admin'         => 'App\Http\Middleware\AdminMiddleware',
        'superadmin'    => 'App\Http\Middleware\SuperAdminMiddleware',
    ];