我已經做中間件,檢查是否存在類按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之前的中間件是否按照路徑搜索控制器類?
爲什麼你會這樣做? – TheFallen