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

mehany's avatar
Level 13

Just use the web middleware by default!

What if I want to use other middleWares!? I tried the bellow and I get tokenMismatchException

 Route::get('/', function () {
     return view('welcome');
 });
 Route::group(['middleware' => 'api'], function () {
     Route::get('/form', function(){
         return view('form');
     });
     Route::post('/test', 'TestController@test');
     Route::put('/test', 'TestController@test');
 
 });

Html

   <form action="/test" method="POST">
        <input type="hidden" name="_method" value="PUT">
        <input type="text" >
        <button type="submit">Submit test</button>
         </form>

P.S: is it because I am making a put request? or am I missing something!?

0 likes
11 replies
JeffreyWay's avatar

Yeah, that won't work. Go to your app/providers/RouteServiceProvider.php file and update it however you need:

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

Two options, maybe:

  1. Just remove the 'middleware' => 'web' block entirely and continue like you used to.
  2. Add a whole new $router->group() section that loads, say, Http/routes-api.php.
3 likes
mehany's avatar
Level 13

@ricardosoares

Just remove the 'middleware' => 'web' block entirely and continue like you used to.

P.s: I actually posted this question too quickly. People have been doing this already but nice to have out of the box. i prefer the second option

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

         $router->group([
             'namespace' => $this->namespace,
         ], function ($router) {
             require app_path('Http/exposed-routes.php');
         });
     }
2 likes
Prullenbak's avatar

Add a whole new $router->group() section that loads, say, Http/routes-api.php.

That should've been the default!

so, maybe add something like this to the RouteServiceProvider (untested, will try it later this week and maybe make a PR)

    /**
     * Define the "api" routes for the application.
     *
     *
     * @param  \Illuminate\Routing\Router  $router
     * @return void
     */
    protected function mapApiRoutes(Router $router)
    {
        $router->group([
            'namespace' => $this->namespace, 'middleware' => 'api',
        ], function ($router) {
            require app_path('Http/routes-api.php');
        });
    }

and change the map function to:

    public function map(Router $router)
    {
        $this->mapWebRoutes($router);
        $this->mapApiRoutes($router);
        //
    }
3 likes
psteenbergen's avatar

@Prullenbak used your way to create that, and it works. Just to let you know. A Pull Request would be great for it. I think it helps a lot of people.

Prullenbak's avatar

It wasn't that hard, so I made a PR for it. Never made one before, so here's hoping I did it right :P

psteenbergen's avatar

It isn't hard indeed, but maybe many people are overlooking it aswell. I didn't expect this to happen, after a clean install of Laravel. Previous week making a new project was fine.. It makes you head scratch in the beginning over the changes.

1 like
bashy's avatar

I prefer using this

/**
 * Define the routes for the application.
 *
 * @param  \Illuminate\Routing\Router  $router
 * @return void
 */
public function map(Router $router)
{
    $router->group(['namespace' => $this->namespace], function ($router) {
        foreach (glob(app_path('Http//Routes') . '/*.php') as $file) {
            $this->app->make('App\\Http\\Routes\\' . basename($file, '.php'))->map($router);
        }
    });
}
1 like
ardf16's avatar

Well that's awesome but little confusing at first. So what's happening here is we are adding new file with routes and assign global middleware to it's routes right?

Please or to participate in this conversation.