ReflectionException - 在Laravel 5上创建http中间件后,类不存在

时间:2016-04-29 09:17:20

标签: php laravel http laravel-5 middleware

我只是想添加HTTP中间件来过滤用户角色,但是我收到了这个错误:

  

Container.php第779行中的ReflectionException:类create:comptable   不存在

in Container.php line 779
at ReflectionClass->__construct('create:comptable') in Container.php line 779
at Container->build('create:comptable', array()) in Container.php line 659
at Container->make('create:comptable', array()) in Application.php line 644
at Application->make('create:comptable') in Pipeline.php line 124
at Pipeline->Illuminate\Pipeline\{closure}(object(Request))
at call_user_func(object(Closure), object(Request)) in Pipeline.php line 101
at Pipeline->then(object(Closure)) in Router.php line 703
at Router->runRouteWithinStack(object(Route), object(Request)) in Router.php line 670
at Router->dispatchToRoute(object(Request)) in Router.php line 628
at Router->dispatch(object(Request)) in Kernel.php line 214
at Kernel->Illuminate\Foundation\Http\{closure}(object(Request))
at call_user_func(object(Closure), object(Request)) in Pipeline.php line 141
at Pipeline->Illuminate\Pipeline\{closure}(object(Request)) in VerifyCsrfToken.php line 43
at VerifyCsrfToken->handle(object(Request), object(Closure)) in VerifyCsrfToken.php line 17
at VerifyCsrfToken->handle(object(Request), object(Closure)) in Pipeline.php line 125
at Pipeline->Illuminate\Pipeline\{closure}(object(Request)) in ShareErrorsFromSession.php line 55
at ShareErrorsFromSession->handle(object(Request), object(Closure)) in Pipeline.php line 125
at Pipeline->Illuminate\Pipeline\{closure}(object(Request)) in StartSession.php line 61
at StartSession->handle(object(Request), object(Closure)) in Pipeline.php line 125
at Pipeline->Illuminate\Pipeline\{closure}(object(Request)) in AddQueuedCookiesToResponse.php line 36
at AddQueuedCookiesToResponse->handle(object(Request), object(Closure)) in Pipeline.php line 125
at Pipeline->Illuminate\Pipeline\{closure}(object(Request)) in EncryptCookies.php line 40
at EncryptCookies->handle(object(Request), object(Closure)) in Pipeline.php line 125
at Pipeline->Illuminate\Pipeline\{closure}(object(Request)) in CheckForMaintenanceMode.php line 42
at CheckForMaintenanceMode->handle(object(Request), object(Closure)) in Pipeline.php line 125
at Pipeline->Illuminate\Pipeline\{closure}(object(Request))
at call_user_func(object(Closure), object(Request)) in Pipeline.php line 101
at Pipeline->then(object(Closure)) in Kernel.php line 115
at Kernel->sendRequestThroughRouter(object(Request)) in Kernel.php line 84
at Kernel->handle(object(Request)) in index.php line 53
at require_once('C:\wamp\www\Medecin2016\public\index.php') in server.php line 21

这就是我所做的: 创建middlerware"创建":

<?php namespace App\Http\Middleware;

use Closure;

class CreateInfos {

    /**
     * Handle an incoming request.
     *
     * @param  \Illuminate\Http\Request  $request
     * @param  \Closure  $next
     * @return mixed
     */
    public function handle($request, Closure $next,$comptable)
    {
        $User = $request->user();
        return ($User->hasRole($comptable)) ? $next($request) : response(view('errors.503'),401);
    }

}

通过编辑Kernel.php文件设置短名称:

<?php namespace App\Http;

use Illuminate\Foundation\Http\Kernel as HttpKernel;

class Kernel extends HttpKernel {

    /**
     * The application's global HTTP middleware stack.
     *
     * @var array
     */
    protected $middleware = [
        'Illuminate\Foundation\Http\Middleware\CheckForMaintenanceMode',
        'Illuminate\Cookie\Middleware\EncryptCookies',
        'Illuminate\Cookie\Middleware\AddQueuedCookiesToResponse',
        'Illuminate\Session\Middleware\StartSession',
        'Illuminate\View\Middleware\ShareErrorsFromSession',
        'App\Http\Middleware\VerifyCsrfToken',
    ];

    /**
     * The application's route middleware.
     *
     * @var array
     */
    protected $routeMiddleware = [
        'auth' => 'App\Http\Middleware\Authenticate',
        'auth.basic' => 'Illuminate\Auth\Middleware\AuthenticateWithBasicAuth',
        'guest' => 'App\Http\Middleware\RedirectIfAuthenticated',
        'create' => 'App\Http\Middleware\CreateInfos',
        'update' => 'App\Http\Middleware\UpdateInfos',
        'delete' => 'App\Http\Middleware\DeleteInfos',
    ];

}

将中间件添加到Routes文件中:

Route::get('profile/diplomes'           ,['middleware'=>'create:comptable','uses'=>'ProfileFormsController@getFormDiplomes']);
Route::post('profile/diplomes'          ,['middleware'=>'create:comptable','uses'=>'ProfileFormsController@postDiplomes']);
Route::post('profile/update/diplomes'   ,['middleware'=>'create:comptable','uses'=>'ProfileFormsController@updateDiplomes']);
Route::post('profile/delete/diplomes'   ,['middleware'=>'create:comptable','uses'=>'ProfileFormsController@deleteDiplomes']);

1 个答案:

答案 0 :(得分:0)

将路由器中的中间件更改为控制器并将其添加到其中:

cnvrtToImg