搜尋

首頁  >  問答  >  主體

laravel - 為什麼使用這個中間件後所有的路由都打不開了?

打開所有的路由都顯示重定向次過過多 這個是為什麼?

Route::group([],function(){
    Route::get('/', function () {
        return view('shouye');
    });
    Route::get('/123', array('as'=>'list','uses'=>function () {
        return route('list');
    }));
    Route::get('/456', ['middleware'=>'login','uses'=>function () {
        return 456;
    }]);
});
<?php

namespace App\Http\Middleware;

use Closure;

class LoginMiddleware
{
    /**
     * Handle an incoming request.
     *
     * @param  \Illuminate\Http\Request  $request
     * @param  \Closure  $next
     * @return mixed
     */
    public function handle($request, Closure $next)
    {
        if(!session('uid')){
            return redirect('/123');
        }
        return $next($request);
    }
}
protected $middleware = [
        \Illuminate\Foundation\Http\Middleware\CheckForMaintenanceMode::class,
        \App\Http\Middleware\LoginMiddleware::class,
    ];
    
protected $routeMiddleware = [
        'auth' => \App\Http\Middleware\Authenticate::class,
        'auth.basic' => \Illuminate\Auth\Middleware\AuthenticateWithBasicAuth::class,
        'guest' => \App\Http\Middleware\RedirectIfAuthenticated::class,
        'throttle' => \Illuminate\Routing\Middleware\ThrottleRequests::class,
        'login'=>\App\Http\Middleware\LoginMiddleware::class,
    ];    
phpcn_u1582phpcn_u15822714 天前765

全部回覆(1)我來回復

  • 習慣沉默

    習慣沉默2017-06-22 11:56:12

    樓主把LoginMiddleware這個中間件加在了Http/Kernel.php檔案中的$middleware變數中,意味每次要求都會經過這個路由;

    這樣每次都會重定義向 /123這個路由上;而/123自身這個請求,也需要通過LoginMiddleware這個中間件,自然就是死循環了;

    回覆
    0
  • 取消回覆