简体   繁体   English

通过auth中间件重定向到自定义登录页面

[英]Redirect to custom login page by auth middleware

Without generating Laravel default auth controllers, I create my own login and registration controller. 在不生成Laravel默认auth控制器的情况下,我创建了自己的登录和注册控制器。

My routes look like these 我的路线看起来像这些

For Login 登录

GET   /login   ->> SessionController@create -->name = login.create
POST  /login   ->> SessionController@store -->name = login.store

For Registration 注册

GET     /registration   ->> RegistrationController@create  -->name = register.create
POST    /registration   ->> RegistrationController@store   -->name = register.store

Everything is fine. 一切都好。 Now I need one thing. 现在我需要一件事。 Whenever I put auth middleware, a non-authenticated user will be redirected to my custom login page route to login.create . 每当我放置auth中间件时,未经过身份验证的用户将被重定向到我的自定义登录页面路由到login.create

How to do this? 这该怎么做?

You can extend Illuminate\\Auth\\Middleware\\Authenticate middleware or just add a new middleware: 您可以扩展Illuminate\\Auth\\Middleware\\Authenticate中间件或只添加一个新的中间件:

class AuthMiddleware
{
    public function handle($request, Closure $next)
    {
        if (auth()->guest()) {
            return redirect()->route('login.create');
        }

        return $next($request);
    }
}

Register it in App\\Http\\Kernel.php : App\\Http\\Kernel.php注册它:

protected $routeMiddleware = [
    'auth.custom' => \App\Http\Middleware\AuthMiddleware::class,
    ....

And use: 并使用:

Route::group(['middleware' => 'auth.custom'], function () {
    // Protected routes.
});

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM