thomthom's avatar

I also got bitten by this. The Laravel docs doesn't make it clear what one need to disable the omni-present web middleware in order to create an api route.

Given how Laravel set up things to be easy for most common tasks - it would be nice to include scaffolding to add REST API end-points.

thomthom's avatar

So here's what I ended up doing:

    // RouteServiceProvider.php
    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/api.php');
        });
    }

That let me separate API routes in a separate file - which makes sense. But it would be nice if the docs where clearer about this.

luqmanrom's avatar

This solution is from this question from Stackoverflow. It looks more cleaner and makes sense

public function map(Router $router)
{
    $this->mapWebRoutes($router);
    $this->mapApiRoutes($router);

    //
}

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

// Add this method and call it in map method. 
protected function mapApiRoutes(Router $router)
{
    $router->group([
        'namespace' => $this->namespace, 'middleware' => 'api',
    ], function ($router) {
        require app_path('Http/routes-api.php');
    });
}
2 likes
iraklisg's avatar

Hello all,

I have the following question. When I run composer-update to update the framework, the composer successfully downloads and installs the new packages in /vendor. However, the App\RouteServiceProvider.php file is not updated (so that to include the web middleware ).

Should I just manually copy-paste the most recent code from github to my App\RouteServiceProvider.php, or am I missing something during the composer update procedure?

Thank you

Snapey's avatar

@iraklisg You can carry on including the web middleware in your routes file as you presumably have already?

iraklisg's avatar

Currently I am using web middleware explicitly, by applying it to group routes like this:

Route::group(['middleware' => 'web'], function () {
    Route::get( ... );
});

However, what I am really asking is why, although I am composer-updating my project to 5.2.39, the App\RouteServiceProvider.php file has not been updated to apply the web middleware in all routes.

This is my RouteServiceProvider.php map() function (after updating to 5.2.39)

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

Apparently this is not the same as the one in official laravel's github page here.

Should I update it manually? Why has not been updated when running composer-update ?

mikevrind's avatar

Only the code in vendor directory will be updated by composer. A composer update won't affect the Laravel setup itself. You should update manually if you would like to have these changes.

Previous

Please or to participate in this conversation.