vendredi 29 avril 2016

ReflectionException - Class does not exist after creating http middleware on Laravel 5

I just want to add HTTP middleware for filtering user role but I get this error:

ReflectionException in Container.php line 779: Class create:comptable does not exist

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

And this is what I do: Create middlerware "create":

<?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);
    }

}

Set a short name by editing Kernel.php file:

<?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',
    ];

}

And adding the middleware to Routes file:

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']);

And this is a part of my controller "ProfileFormsController":

<?php namespace App\Http\Controllers;

use App\Http\Requests;
use App\Http\Controllers\Controller;
use Auth;
use Illuminate\Http\Request;
use App\Http\Requests\ContactRequest;

class ProfileFormsController extends Controller {
    public function __construct()
    {
        $this->middleware('auth');
    }

    public function postDiplomes(Request $request)
    {
        if($request->ajax() && Auth::user()->id==$request->input('id')){
            $this->validate($request, [
                'diplomes'  => 'required',
                'lieu'      => 'required',
                'annee'     => 'required|date_format:d/m/Y',
            ]); 

            $dipl= new \App\Diplome;
            $dipl->diplomes=$request->input('diplomes');
            $dipl->lieu=$request->input('lieu');
            $dipl->annee=$request->input('annee');
            $dipl->save();

            $user=\App\User::find($request->input('id'));
            $user->diplomes()->attach($dipl->id);
            return 'saved';
        }
    }

    public function updateDiplomes(Request $request)
    {
        if($request->ajax()){
            $this->validate($request, [
                'diplomes'  => 'required',
                'lieu'      => 'required',
                'annee'     => 'required|date_format:d/m/Y',
            ]); 

            $dipl=\App\Diplome::find($request->input('id'));
            $dipl->diplomes=$request->input('diplomes');
            $dipl->lieu=$request->input('lieu');
            $dipl->annee=$request->input('annee');
            $dipl->save();

            return ' saved';
        }
    }


    public function deleteDiplomes(Request $request)
    {
        if($request->ajax()){
            \App\Diplome::destroy($request->input('id'));
            return 'saved';
        }
    }



from Newest questions tagged laravel-5 - Stack Overflow http://ift.tt/1T9q3gq
via IFTTT

Aucun commentaire:

Enregistrer un commentaire