php 如何更改 Laravel 5 Auth 过滤器的默认重定向 URL?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/28702372/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-26 00:00:54  来源:igfitidea点击:

How to change default redirect URL of Laravel 5 Auth filter?

phplaravel-5

提问by Vladimir

By default if I am not logged and I try visit this in browser:

默认情况下,如果我没有登录并尝试在浏览器中访问它:

http://localhost:8000/home

It redirect me to http://localhost:8000/auth/login

它将我重定向到 http://localhost:8000/auth/login

How can I change to redirect me to http://localhost:8000/login

如何更改以将我重定向到 http://localhost:8000/login

回答by Moritz Ewert

I wanted to do the same thing in Laravel 5.5. Handling authentication has moved to Illuminate\Auth\Middleware\Authenticatewhich throws an Illuminate\Auth\AuthenticationException.

我想在 Laravel 5.5 中做同样的事情。处理身份验证已移至Illuminate\Auth\Middleware\Authenticate抛出Illuminate\Auth\AuthenticationException.

That exception is handled in Illuminate\Foundation\Exceptions\Hander.php, but you don't want to change the original vendor files, so you can overwrite it with your own project files by adding it to App\Exceptions\Handler.php.

该异常在 中处理Illuminate\Foundation\Exceptions\Hander.php,但您不想更改原始供应商文件,因此您可以通过将其添加到App\Exceptions\Handler.php.

To do this, add the following to the top of the Handlerclass in App\Exceptions\Handler.php:

为此,请将以下内容添加到 中的Handler类顶部App\Exceptions\Handler.php

use Illuminate\Auth\AuthenticationException;

And then add the following method, editing as necessary:

然后添加以下方法,根据需要进行编辑:

/**
 * Convert an authentication exception into an unauthenticated response.
 *
 * @param  \Illuminate\Http\Request  $request
 * @param  \Illuminate\Auth\AuthenticationException  $exception
 * @return \Illuminate\Http\Response
 */
protected function unauthenticated($request, AuthenticationException $exception)
{
    if ($request->expectsJson()) {
        return response()->json(['error' => 'Unauthenticated.'], 401);
    }

    return redirect()->guest('login'); //<----- Change this
}

Just change return redirect()->guest('login');to return redirect()->guest(route('auth.login'));or anything else.

只需更改return redirect()->guest('login');return redirect()->guest(route('auth.login'));或其他任何内容。

I wanted to write this down because it took me more than 5 minutes to figure it out. Please drop me a line if you happened to find this in the docs because I couldn't.

我想把它写下来,因为我花了超过 5 分钟才弄明白。如果您碰巧在文档中找到了这个,请给我留言,因为我找不到。

回答by e1v

Just to extend @ultimate's answer:

只是为了扩展@ultimate的回答:

  1. You need to modify App\Http\Middleware\Authenticate::handle()method and change auth/loginto /login.
  2. Than you need to add $loginPathproperty to your \App\Http\Controllers\Auth\AuthControllerclass. Why? See Laravel source.
  1. 您需要修改App\Http\Middleware\Authenticate::handle()方法并更改auth/login/login.
  2. 比您需要$loginPath为您的\App\Http\Controllers\Auth\AuthController班级添加属性。为什么?请参阅Laravel 源代码

In result you'll have this in your middleware:

结果你会在你的中间件中有这个:

namespace App\Http\Middleware;
class Authenticate {
        /**
     * Handle an incoming request.
     *
     * @param  \Illuminate\Http\Request  $request
     * @param  \Closure  $next
     * @return mixed
     */
    public function handle($request, Closure $next)
    {
        if ($this->auth->guest())
        {
            if ($request->ajax())
            {
                return response('Unauthorized.', 401);
            }
            else
            {
                return redirect()->guest('/login'); // <--- note this
            }
        }

        return $next($request);
    }
}

And this in your AuthController:

这在您的 AuthController 中:

namespace App\Http\Controllers\Auth;
class AuthController extends Controller
{
    protected $loginPath = '/login'; // <--- note this

    // ... other properties, constructor, traits, etc 
}

回答by Salil Kothadia

This is Laravel 5.4 Solution:

这是 Laravel 5.4 解决方案:

There is a new unauthenticated() method in app/Exceptions/Handler.php which handles unauthenticated users and redirects to login path.

app/Exceptions/Handler.php 中有一个新的 unauthenticated() 方法,它处理未经身份验证的用户并重定向到登录路径。

So change

所以改变

return redirect()->guest('login');

to

return redirect()->guest('auth/login');

回答by Darryldecode

In Laravel 5.6, go to app/Exceptions folder and open the Handler.php, add a new method that overrides the unauthenticated method like so:

在 Laravel 5.6 中,转到 app/Exceptions 文件夹并打开 Handler.php,添加一个覆盖未经身份验证的方法的新方法,如下所示:

protected function unauthenticated($request, AuthenticationException $exception)
{
    if($request->ajax())
    {
        return response([
            "message" => "Unauthenticated.",
            "data" => [],
        ],401);
    }

    return redirect()->to('/');
}

This method is triggered when you access a protected route using the built-in "auth" middleware. Now you will have full control where to redirect or the response sent.

当您使用内置的“auth”中间件访问受保护的路由时,会触发此方法。现在您将完全控制重定向或发送响应的位置。

回答by ultimate

Authentication checks are made using middleware in Laravel 5.

在 Laravel 5 中使用中间件进行身份验证检查。

And the middleware for auth is App\Http\Middleware\Authenticate.

身份验证的中间件是App\Http\Middleware\Authenticate.

So, you can change it in handlemethod of the middleware.

因此,您可以handle在中间件的方法中更改它。

回答by marco8757

EDIT: On Laravel 5.1, simply add protected $redirectPath = '/url/you/want';to AuthController would do the trick.

编辑:在 Laravel 5.1 上,只需添加protected $redirectPath = '/url/you/want'; 到 AuthController 就可以了。

REFER : http://laravel.com/docs/5.1/authentication#included-authenticating

参考:http: //laravel.com/docs/5.1/authentication#included-authenticating



On Laravel 5.1, it is completely moved to another middleware named RedirectIfAuthenticated.phpunder App\Http\Middleware

在 Laravel 5.1 上,它完全移到了App\Http\Middleware下名为RedirectIfAuthenticated.php 的另一个中间件

public function handle($request, Closure $next)

{
    if ($this->auth->check()) {
        return redirect('/'); //change this part to anywhere you wish to be redirected to
    }

    return $next($request);
}

Hope it helps.

希望能帮助到你。

回答by Nikunj K.

could you please outputs php artisan route:listplease

你可以请输出php artisan route:list

You are right you can set the following attributes:

您是对的,您可以设置以下属性:

protected $loginPath = 'xxx';

protected $redirectPath = 'xxx';

protected $redirectAfterLogout = 'xxx';

Set this attribute to you AuthController.php

将此属性设置为您 AuthController.php

回答by JLPuro

Since your other questionwas marked as duplicate..I will try to answer it here..

由于您的另一个问题被标记为重复..我会在这里尝试回答..

First you need to change your route like

首先你需要改变你的路线

<?php
Route::get(config('constants.cms_path') . '/login', [
    'as' => 'login',
    'uses' => 'Auth\AuthController@getLogin'
]);

In your blade..make sure you use named route in the Login url link like

在您的刀片中..确保您在登录 url 链接中使用命名路由,例如

{{ route('login') }}

In Middleware/Authenticate.php change the redirect guest to

在 Middleware/Authenticate.php 中,将重定向来宾更改为

return redirect()->guest(config('constants.cms_path') . '/login');

回答by asanchez

To change the redirection after the login, you only have to go to app/Http/Controllers/Auth/LoginController.php and add that inside the class LoginController:

要在登录后更改重定向,您只需转到 app/Http/Controllers/Auth/LoginController.php 并将其添加到类 LoginController 中:

protected $redirectTo = '/redirect-url-here';

Same for redirection after a new users register, but in that case, on AuthController.php

新用户注册后重定向相同,但在这种情况下,在 AuthController.php

回答by Daud khan

For Laravel 5.4 You can set protected $redirectTo = '/'; in LoginController.php FILE. Or in RegistersUsers.php file you can

对于 Laravel 5.4 你可以设置 protected $redirectTo = '/'; 在 LoginController.php 文件中。或者在 RegistersUsers.php 文件中,您可以

protected function registered(Request $request, $user)
{
    return redirect('tosomeRoute'); 
    //Note: This code will run when
    //The user has been registered
}