2017-07-27 88 views
20

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')); 
} 
+0

https://laravel-news.com/custom-exception-reporting我发现了这个 –

+0

所以你可以捕获身份验证错误,然后在被laravels抓住之前重定向。非常感谢 – robertmylne

+0

@robertmylne工作吗?因为这个解决方案只是为了报告! – Maraboc

回答

28

但在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')); 
    } 
} 
+1

最佳答案我找到了,谢谢!只有一件事:有没有方法使用expectsJson(),检查请求是否通过API路线? –

+0

你是真棒男人,我想禁用网络中间件,但这种解决方案是最好的。 –

+1

'if(in_array('api',\ Route :: getCurrentRoute() - > computedMiddleware)){return error() - > json(['error'=>'Unauthenticated。',401); }' – paing

4

下面是我解决它的方法。 在渲染函数中,我捕获了异常类。如果它是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); 
} 
+0

感谢它为我工作。我在YouTube上使用了DevMarketer MultiAuth教程,但它似乎不适用于laravel 5.5。这解决了我的问题。 –

+0

与我合作2,并且我正在起诉devMarketer ...世界太小 –

4

但在Laravel 5.5本已被移动到这个位置 供应商/ laravel /框架/ src目录/照亮/基金/例外/处理器。 php 所以我现在怎么改变它?我不想更改供应商 目录中的内容,它会被作曲者更新覆盖。

我们只需要包含 使用Illuminate \ Auth \ AuthenticationException;

,然后它工作在laravel 5.4

+0

谢谢,这个解决方案在Laravel 5.5中适用于我:) –

-4

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返回。

+0

从外部包中修改文件是非常糟糕的做法!不要这样做。 – thephper

1

除了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; 
    } 
} 
0

只需添加一个路由,登录在routes文件:

Route::get('/login', [ 
    'uses' => '[email protected]', 
    'as' => 'login' 
]); 
0

标准的异常处理程序使用名为路线。

所以,你只需定义你的路线使用该名称。

所以,在你的路由/ 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();”时添加在路由文件中。