laravel - 为什么使用这个中间件后所有的路由都打不开了?
phpcn_u1582
phpcn_u1582 2017-06-22 11:54:12
0
1
762

打开所有的路由都显示重定向次过过多 这个是为什么?

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_u1582
phpcn_u1582

全部回复(1)
習慣沉默

楼主把LoginMiddleware这个中间件加在了Http/Kernel.php文件中的$middleware变量中,意味每次请求都会经过这个路由;

这样每次都会重定义向 /123这个路由上;而/123自身这个请求,也需要通过LoginMiddleware这个中间件,自然就是死循环了;

热门教程
更多>
最新下载
更多>
网站特效
网站源码
网站素材
前端模板