2016-08-02 64 views
0

假設我在routes.php文件有這樣的:現在Laravel 5.2 - 未定義的路由默認錯誤頁

Route::get('sells', ['as' => 'user_sells', 'uses' => '[email protected]']); 

,你也知道,如果有人打開mySite.com/sells,在控制器所需的方法將被執行。

但是,如果有人試圖訪問一個沒有定義的路由,比如mySite.com/buys,我想顯示一個默認的錯誤頁面。我的意思是,如果路線沒有定義,我需要說明一個特定的頁面。

我該怎麼做?

在此先感謝

補充: 當我試圖訪問一個未定義的路線我面對的錯誤:

Whoops, looks like something went wrong.

ErrorException in C:\wamp\www\codes\laravel5\portpapa\vendor\laravel\framework\src\Illuminate\Container\Container.php line 835: Unresolvable dependency resolving [Parameter #0 [ $methods ]] in class Illuminate\Routing\Route (View: ...

回答

3

其實,Laravel已經在默認情況下有這樣的。如果您在resources/views文件夾中創建了一個名爲errors/404.blade.php的視圖,該視圖將會自動執行。

如果你要處理的404錯誤與自定義代碼,正好趕上了NotFoundHttpException例外,在App\Exceptions\Handler類:

public function render($request, Exception $e) 
{ 
    if ($e instanceof \Symfony\Component\HttpKernel\Exception\NotFoundHttpException) { 
     // handle here 
     return response()->view('errors.404', [], 404); 
    } 
} 
+0

我得到這個錯誤:在類Illuminate \ Routing \ Route中無法解決的依賴項解決[Parameter#0 [ $ methods]]。我想我可能在某些地方使用過Route,但我不應該這樣做。 – Mojtaba

3

如果沒有定義的途徑,NotFoundHttpException會拋出。 異常在app/Exceptions/handler.php中的Larevel中進行管理。

您必須檢查例外是否爲NotFoundHttpException,並在此情況下返回正確的視圖。

public function render($request, Exception $e) 
{ 
    if ($this->isHttpException($e)) 
    {  
     if($e instanceof NotFoundHttpException) 
     { 
      return response()->view('my.view', [], 404); 
     } 
     return $this->renderHttpException($e); 
    } 
    return parent::render($request, $e); 
} 

Source