2016-08-22 57 views
0

,以委託,我實現了用戶將如何被重定向到登錄後自己的儀表板由什麼挑戰,我對着右Authcontroller.phpLaravel 5.2委託:重定向用戶,如果他手動添加限制URL

protected function authenticated() 
{ 

    if(\Auth::user()->hasRole(['super_admin',])) { 
     return redirect('/dashboard'); 
    } else if(\Auth::user()->hasRole(['staff_admin'])) { 
     return redirect('/staff/dashboard'); 
    } else if(\Auth::user()->hasRole(['subadmin_admin'])) { 
     return redirect('/subadmin/dashboard'); 
    } 
} 

添加此方法現在是,例如。如果工作人員已登錄並重定向到他的儀表盤爲

domain.com/staff/dashboard

但如果他手動從URL刪除工作人員和試圖訪問超級管理員控制檯然後委託拋出403錯誤,但我想重定向他到他的儀表板,並帶有「您沒有被授權」的消息。

我試圖在RedirectIfAuthenticated中間件中實現相同的代碼,但它給出了錯誤,因爲hasRole在Null上調用。

回答

0

您的DashboardController添加構造函數。

示例代碼

class UserController extends Controller 
{ 
     public function __construct() 
     { 
      $this->middleware(['role:super_admin']); 
     } 

     public function index() 
     { 
     return view('dashboard'); 
     } 
} 

然後,錯誤的文件夾添加403.blade.php像這樣:

<!DOCTYPE html> 
<html> 
    <head> 
     <title>Be right back.</title> 

     <link href="https://fonts.googleapis.com/css?family=Lato:100" rel="stylesheet" type="text/css"> 

     <style> 
      html, body { 
       height: 100%; 
      } 

      body { 
       margin: 0; 
       padding: 0; 
       width: 100%; 
       color: #B0BEC5; 
       display: table; 
       font-weight: 100; 
       font-family: 'Lato', sans-serif; 
      } 

      .container { 
       text-align: center; 
       display: table-cell; 
       vertical-align: middle; 
      } 

      .content { 
       text-align: center; 
       display: inline-block; 
      } 

      .title { 
       font-size: 72px; 
       margin-bottom: 40px; 
      } 
     </style> 
    </head> 
    <body> 
     <div class="container"> 
      <div class="content"> 
       <div class="title">You are not authorized.</div> 
      </div> 
     </div> 
    </body> 
</html> 
+0

NOP!我不是要求得到403錯誤頁面。我想要的是,如果他手動更改URL,那麼如何重定向回他的儀表板,而不是顯示死亡403頁面。希望我清楚的問題。 – Tarunn

+0

您可以在vendor/zizaco/entrust/src/Entrust/Middleware/EntrustRole.php中的句柄方法中覆蓋 –

相關問題