2016-08-03 76 views
0

我已經做中間件,檢查是否存在類按URL段和中間件被觸發laravel捕捉異常之前進行適當tasks.However並引發錯誤:中間件不workign如預期Laravel 5.2

ReflectionException in Route.php line 280: 
Class App\Http\Controllers\cms\modules\asdfasdfsadf\asdfasdfsadfController does not exist 

如果我死亡並轉儲checkClass中間件上的任何東西,則該值僅在該類存在時轉儲。

我的路線,其中應用中間件是:

Route::group(['prefix' => PREFIX,'middleware'=>array('auth','role','checkClass')], function() { 
    Route::group(['namespace' => 'cms' . '\\modules\\' . Request::segment(2) ], function() { 

     Route::get('{module}/pages/{page}', Request::segment(4) . '[email protected]'); 

     Route::get('{module}', Request::segment(2) . '[email protected]'); 

     Route::get('{module}/{page}', Request::segment(2) . '[email protected]' . Request::segment(3)); 

     Route::post('{module}/{page}', Request::segment(2) . '[email protected]' . Request::segment(3)); 

     Route::get('{module}/{page}/{id}', Request::segment(2) . '[email protected]' . Request::segment(3)); 

     Route::get('{module}/pages/{page}/{id}', Request::segment(4) . '[email protected]' . Request::segment(5)); 

     Route::post('{module}/pages/{page}/{id}', Request::segment(4) . '[email protected]' . Request::segment(5)); 

     Route::get('{module}/pages/{page}/{abc}/{id}', Request::segment(4) . '[email protected]' . Request::segment(5)); 

     Route::post('{module}/pages/{page}/{abc}/{id}', Request::segment(4) . '[email protected]' . Request::segment(5)); 
    }); 
}); 

在這裏我們可以看到,我已動態創建的路線爲每個URL segment.Here是我checkClass中間件:

<?php 

namespace App\Http\Middleware; 

use Closure; 
use Request; 
use Illuminate\Contracts\Auth\Guard; 

class CheckClass 
{ 
    /** 
    * The Guard implementation. 
    * 
    * @var Guard 
    */ 
    protected $auth; 

    /** 
    * Create a new filter instance. 
    * 
    * @param Guard $auth 
    * @return void 
    */ 
    public function __construct(Guard $auth) 
    { 
     $this->auth = $auth; 
    } 

    /** 
    * Handle an incoming request. 
    * 
    * @param \Illuminate\Http\Request $request 
    * @param \Closure $next 
    * @return mixed 
    */ 
    public function handle($request, Closure $next) 
    { 

     if (!class_exists('App\\Http\\Controllers\\cms\\modules\\'.Request::segment(2).'\\'.Request::segment(2).'Controller')) { 

       return view("errors.404"); 

     } else { 
      return $next($request); 
     } 

     return $next($request); 
    } 
} 

不應該在laravel之前的中間件是否按照路徑搜索控制器類?

+1

爲什麼你會這樣做? – TheFallen

回答

0

在Laravel找到路由後,中間件被解僱,因爲它需要找出使用哪個中間件(因爲路由可以嵌套在多個組等中)。

但是這個錯誤在默認情況下會導致404錯誤。也許檢查你的異常處理程序,以確保它(並檢查與app.debug設置爲false,因爲它然後顯示這樣的例外,而不是隻顯示一個404.

+0

其中'app.debug' ?這樣我就可以將它設置爲false? – Sumit

+0

它可以在'.env'('APP_DEBUG')文件中設置,也可以直接在'config/app.php'中更改。 –

+0

nope它僅禁用錯誤消息,但我想在路由拋出任何錯誤之前趕上class not found異常 – Sumit