2017-10-18 52 views
0

我在app\ExceptionsHandler.php中有一些代碼檢查URL是否存在,如果不存在,那麼它會檢查數據庫是否是自定義URL,如果是的話就會返回一個視圖。我認爲這個問題是因爲它沒有通過網絡中間件傳遞,所以會話和身份驗證都沒有啓動,因爲Auth::id()爲空,即使我100%登錄Laravel 5缺少頁面和authed用戶

是否有一個更容易如何做到這一點?

public function render($request, Exception $exception) 
{ 
    if($exception instanceof NotFoundHttpException) 
    { 
     if(\App\EventMeter::isURL()) { 
      $ec = new EventsController(); 
      return $ec->eventMeter(); 
     } 
     if(\App\Tournament::isTournamentURL()) { 
      $t = new TournamentController(); 
      return $t->home(); 
     } 
     if(BountyHunter::isURL()) 
     { 
      $bhc = new BountyHunterController(); 
      return $bhc->home(); 
     } 

     return response()->view('errors.missing',array(), 404); 
    } 

    return parent::render($request, $exception); 
} 
+0

我想因爲你實例化了控制器!只是重定向當前請求,像這樣'return redirect() - > route('route.name');' – Maraboc

+0

這並不能解決問題,因爲URL總是動態的。如果我要讓路由成爲一個靜態的URL,那麼這將工作正常,但URL永遠不會保持不變 – Ben

+0

命中此方法'$ ec-> eventMeter()'的URL會改變? – Maraboc

回答

3

也許這是你應該把責任轉移到你的路由。


Route::get('{uri}', ['uses' => '[email protected]', 'as' => 'page']) 
     ->where('uri', '.*'); 

控制器可以處理所有的意見,並可以執行路由和重定向,讓你有任何其他的路線,你所需要的。通過創建一個類,例如一個LayoutService,它可以處理所有您可以重定向到適當位置的不同事件,錦標賽,賞金。這將讓您可以訪問所有的網絡中間件。

// Logic to determine what method you want to be called 
$template = 'events|tournament|etc'; 

if (method_exists($this->layoutService, $template) 
     && is_callable(array($this->layoutService, $template))) { 
     return call_user_func(
     array($this->layoutService, $template) 
    ); 
}