2

所以我有我的身份验证中间件,它注册Http/Kernel.php为:

protected $routeMiddleware = [
    'auth' => \App\Http\Middleware\Authenticate::class,
    'auth.basic' => \Illuminate\Auth\Middleware\AuthenticateWithBasicAuth::class,
    'guest' => \App\Http\Middleware\RedirectIfAuthenticated::class,
];

接下来我对 Authenticate 类中的中间件句柄函数进行了更改:

public function handle($request, Closure $next)
{
    if ($this->auth->check()) {
        $user = $this->auth->user();

        $currentDateTime     = strtotime('Y-m-d H:i:s');
        $tokenExpirationTile = strtotime($user->token_expiration);

        if ($currentDateTime <= $tokenExpirationTile) {
            return $next($request);
        } else {
            $this->auth->logout();
            redirect('home/login')->with('message', 'Your session has expired. Please login in again');
        }
    } else {
        redirect('home/login')->with('message', 'Please login before attempting to access that');
    }
}

最后我创建了路线:

Route::get('home/dashboard', 'HomeController@dashboard', ['middleware' => 'auth']);

我可以访问这条路线,但作为未登录用户,我应该被重定向。

当我通过函数dd()中的 a 时,handle什么也没有发生。

我如何让它在这条路线上运行这个方法?

此外,当涉及到需要在每个操作请求之前进行身份验证的其他控制器时,您怎么说:“在每个操作之前,运行此方法。” 在rails我会做before_action :method_name

4

1 回答 1

1

对于您问题的第二部分,请查阅文档以了解如何将中间件应用于控制器和路由中的特定操作:

http://laravel.com/docs/master/controllers#controller-middleware http://laravel.com/docs/master/routing#route-group-middleware

对于第一部分,您是否尝试过从终端运行“composer dump-autoload”?

于 2015-09-20T21:41:42.027 回答