Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

KamilKielczewski's avatar

Laravel 5.2 routing - does middelware 'web' is always execute?

I will copy my question (but not exactly in the same way) from http://stackoverflow.com/questions/38405921/laravel5-2-unwanted-verifycsrftoken/38406255#38406255

I set up fresh L5.2 and my route files after changes looks like that:

<?php

Route::get('/', function () {
    return view('welcome');
});

Route::group(['middleware' =>'api', 'prefix' => '/api/v1'], function () {
    Route::post('/api/v1/login', 'Api\V1\Auth\AuthController@postLogin');
});

I create controller and thats all. When i go to postman and make POST: http://myproject.dev/api/v1/login I get: TokenMismatchException in VerifyCsrfToken.php line 67. But I don't newer use 'web' middelware group in routing file (which is definced in Kernel.php) ! So how it works? Does 'web' middelware group is executes for each request (which is counterintuitive)- or may be I do something wrong? Please help

0 likes
3 replies
aardalich's avatar
Level 12

Yes, it is.

It was removed from RouteServiceProvider so you had to explicitly assign the web middleware within routes or controllers for a while but I think it was added back in for approachability for newer people to the framework and it's usually the typical behaviour for a new project.

Remove it from your project's RouteServiceProvider and add web back into any routes or controllers where you need sessions etc

1 like
KamilKielczewski's avatar

Due to your hint @aardalich I found solution: don't remove RouteServiceProvider (because then for instance php artisan route:list will not work) but instead edit in your project app/Providers/RouteServiceProvider@mapWebRoutes and remove 'middleware' => 'web', so you will get:

    protected function mapWebRoutes(Router $router)
    {
        $router->group([
            //'namespace' => $this->namespace, 'middleware' => 'web',
            'namespace' => $this->namespace,
        ], function ($router) {
            require app_path('Http/routes.php');
        });
    }

And any for any request 'web' middelwar group will NOT be executed (which you can see using php artisan route:list )

aardalich's avatar

Sounds like this will change again in 5.3. (laravel podcast)

2 route files, one for web and one for api

1 like

Please or to participate in this conversation.