Laravel 5.5改变未经验证的登录重定向URL
在Laravel < 5.5
我可以改变这个文件app/Exceptions/Handler
改变未经身份验证的用户重定向URL:Laravel 5.5改变未经验证的登录重定向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
所以我能怎样改变现在?我不想更改供应商目录中的东西,并且它会被作曲者更新覆盖。
protected function unauthenticated($request, AuthenticationException $exception)
{
return $request->expectsJson()
? response()->json(['message' => 'Unauthenticated.'], 401)
: redirect()->guest(route('login'));
}
但在Laravel 5.5本已被移动到这个位置,供应商/ laravel /框架/ src目录/照亮/基金/例外/ Handler.php凭什么我现在改变了吗?我不想更改供应商目录中的东西,并且它会被作曲者更新覆盖。
只是这样的情况下,该功能不再是默认情况下。
您可以像在5.4中那样重写它。只要确保在处理程序文件中包含
use Exception;
use Request;
use Illuminate\Auth\AuthenticationException;
use Response;
。
比如我app/Exceptions/Handler.php
看起来有点像这样:
<?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'));
}
}
最佳答案我找到了,谢谢!只有一件事:有没有方法使用expectsJson(),检查请求是否通过API路线? –
你是真棒男人,我想禁用网络中间件,但这种解决方案是最好的。 –
'if(in_array('api',\ Route :: getCurrentRoute() - > computedMiddleware)){return error() - > json(['error'=>'Unauthenticated。',401); }' – paing
下面是我解决它的方法。 在渲染函数中,我捕获了异常类。如果它是Authentication异常类,我写了我的代码重定向(代码,我会在以前的版本中写入未经验证的函数)。
public function render($request, Exception $exception)
{
$class = get_class($exception);
switch($class) {
case 'Illuminate\Auth\AuthenticationException':
$guard = array_get($exception->guards(), 0);
switch ($guard) {
case 'admin':
$login = 'admin.login';
break;
default:
$login = 'login';
break;
}
return redirect()->route($login);
}
return parent::render($request, $exception);
}
感谢它为我工作。我在YouTube上使用了DevMarketer MultiAuth教程,但它似乎不适用于laravel 5.5。这解决了我的问题。 –
与我合作2,并且我正在起诉devMarketer ...世界太小 –
但在Laravel 5.5本已被移动到这个位置 供应商/ laravel /框架/ src目录/照亮/基金/例外/处理器。 php 所以我现在怎么改变它?我不想更改供应商 目录中的内容,它会被作曲者更新覆盖。
我们只需要包含 使用Illuminate \ Auth \ AuthenticationException;
,然后它工作在laravel 5.4
谢谢,这个解决方案在Laravel 5.5中适用于我:) –
1.Go到vendor/laravel/framework/src/Illuminate/Foundation/Exceptions/Handler.php
文件。
2.搜索方法名称为unauthenticated
。
3.将重定向url从 redirect()->guest(route('login'))
更改为redirect()->guest(route('api/login')) //whatever you want
。
如果是API服务,您可以将响应作为JSON返回。
从外部包中修改文件是非常糟糕的做法!不要这样做。 – thephper
除了overriding
,你可以直接作出Handler.php
现有功能未经验证的位于vendor/laravel/framework/src/Illuminate/Foundation/Exceptions/Handler.php
重定向到基于卫士意路由变化。
/**
* 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)
{
$guard = array_get($exception->guards(),0);
switch ($guard) {
case 'admin':
return $request->expectsJson()
? response()->json(['message' => $exception->getMessage()], 401)
: redirect()->guest(route('admin.login'));
break;
default:
return $request->expectsJson()
? response()->json(['message' => $exception->getMessage()], 401)
: redirect()->guest(route('login'));
break;
}
}
标准的异常处理程序使用名为路线。
所以,你只需定义你的路线使用该名称。
所以,在你的路由/ web.php文件,只需添加一行:
Route::get('mylogin', '[email protected]')->name('login');
“名称(‘登录’)”位使这条路的名称,所以未经验证的异常将使用此路线。
您不必费心打造自己的异常处理程序,或修改标准异常处理程序。
可以在'auth()'函数的vendor/laravel/framework/src/Illuminate/Routing/Router.php文件中找到样板'auth'代码使用的命名路由。 (登录,注销,注册,password.request,password.email和password.reset)。这些路由在您使用“Route :: auth();”时添加在路由文件中。
https://laravel-news.com/custom-exception-reporting我发现了这个 –
所以你可以捕获身份验证错误,然后在被laravels抓住之前重定向。非常感谢 – robertmylne
@robertmylne工作吗?因为这个解决方案只是为了报告! – Maraboc