Laravel 5.5更改未经身份验证的登录重定向URL [英] Laravel 5.5 change unauthenticated login redirect url
问题描述
在Laravel < 5.5
中,我可以更改此文件app/Exceptions/Handler
来更改未经身份验证的用户重定向url:
In Laravel < 5.5
I could change this file app/Exceptions/Handler
to change the unauthenticated user redirect url:
protected function unauthenticated($request, AuthenticationException $exception)
{
if ($request->expectsJson()) {
return response()->json(['error' => 'Unauthenticated.'], 401);
}
return redirect()->guest(route('login'));
}
但是在Laravel 5.5
中,此位置已移至该位置vendor/laravel/framework/src/Illuminate/Foundation/Exceptions/Handler.php
,所以现在如何更改它?我不想更改供应商目录中的内容,以防它被作曲家更新所覆盖.
But in Laravel 5.5
this has been moved to this location vendor/laravel/framework/src/Illuminate/Foundation/Exceptions/Handler.php
so how can I change it now? I don't want to change stuff in the vendor directory encase it gets overridden by composer updates.
protected function unauthenticated($request, AuthenticationException $exception)
{
return $request->expectsJson()
? response()->json(['message' => 'Unauthenticated.'], 401)
: redirect()->guest(route('login'));
}
推荐答案
但是在Laravel 5.5中,该位置已移至该位置vendor/laravel/framework/src/Illuminate/Foundation/Exceptions/Handler.php,所以现在如何更改它?我不想更改vendor目录中的内容,以防它被作曲家更新所覆盖.
But in Laravel 5.5 this has been moved to this location vendor/laravel/framework/src/Illuminate/Foundation/Exceptions/Handler.php so how can I change it now? I don't want to change stuff in the vendor directory encase it gets overridden by composer updates.
只是默认情况下该功能不再存在.
It's just the case that the function is not there by default anymore.
您可以像在5.4中一样重写它.只要确保包含
You can just override it as you did in 5.4. Just make sure to include
use Exception;
use Request;
use Illuminate\Auth\AuthenticationException;
use Response;
在处理程序文件中.
例如,我的app/Exceptions/Handler.php
看起来像这样:
For Example my app/Exceptions/Handler.php
looks somewhat like this:
<?php
namespace App\Exceptions;
use Exception;
use Request;
use Illuminate\Auth\AuthenticationException;
use Response;
use Illuminate\Foundation\Exceptions\Handler as ExceptionHandler;
class Handler extends ExceptionHandler
{
(...) // The dfault file content
/**
* Convert an authentication exception into a response.
*
* @param \Illuminate\Http\Request $request
* @param \Illuminate\Auth\AuthenticationException $exception
* @return \Illuminate\Http\Response
*/
protected function unauthenticated($request, AuthenticationException $exception)
{
return $request->expectsJson()
? response()->json(['message' => 'Unauthenticated.'], 401)
: redirect()->guest(route('authentication.index'));
}
}
这篇关于Laravel 5.5更改未经身份验证的登录重定向URL的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!