DengDeng
DengDeng

Reputation: 543

Remove web middleware in laravel 5.2

I know web middleware group is assigned to every route now. But can someone tell me how to remove if for specefic route? I tried:

class HomeController extends Controller{
    public function __construct(){
        $this->middleware('web',['except'=>[
            'index',
        ]]);

    }
}

And it doesn't work.

Upvotes: 0

Views: 2944

Answers (1)

Frnak
Frnak

Reputation: 6802

Web middleware is now applied to all routes in routes.php. This happens in the RouteServiceProvider map function.

If you have an api for example which should not use web middleware you can go with something like this

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

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

Now every route in routes.php has web middleware and everything in routes-api.php the api middleware

Upvotes: 1

Related Questions