2017-07-27 89 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();」時添加在路由文件中。