Search code examples
phplaravelmiddleware

ReflectionException (-1) Class checkSession does not exist


I have a middleware checkSession for checking sessions in my Laravel Project. But, while serving the Laravel project using php artisan serve, the project is served successfully. But, while using the web application, this error is being thrown ReflectionException (-1) Class checkSession does not exist

I have searched for other answers on SO. But none of them seems to work for my case.

I have also double checked that checkSession class exists in the correct Middleware directory with correct namespace. I am not able to figure why the error is happening. Kindly help.


Solution

  • You should have missed registering the middleware. After defining the middleware, you should register it to a particular route or a group of routes or for all HTTP requests to your application.

    If you don't register a middleware, it won't know on which route the middleware should be activated.

    If you want to register the middleware for all the routes in your application, add checkSession to the $middleware property present in the /app/Http/Kernel.php file.

    Else, if you want to activate the middleware for certain routes, associate a key to the middleware and add it to the existing middlewares present in the $routeMiddleware property in the above-mentioned file.

    For e.g. append this to the $routeMiddleware variable: 'checkSession' => \App\Http\Middleware\checkSession::class, Now, you can use this checkSession key for associating this middleware with specific routes in your route definition.

    I strongly recommend you to read the documentation before implementing. (https://laravel.com/docs/5.6/middleware#registering-middleware)