php – 避免/删除Laravel> = 5.2.31的路由中的Web中间件

前端之家收集整理的这篇文章主要介绍了php – 避免/删除Laravel> = 5.2.31的路由中的Web中间件前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
在此 changes即Laravel 5.2.31及更高版本之后,app / Http / routes.PHP中的所有路由都将属于Web中间件组.

在RouteServiceProvider.PHP

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

问题:

>没有Web中间件定义路由集的最简单/最好的方法是什么?

其中一个用例是,声明无状态api的路由,没有会话中间件属于Web组中间件

解决方法

解决这个问题的一种方法是编辑app / Providers / RouteServiceProvider.PHP并为其他组中间件提供另一个路由文件,即api

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

    //
}

protected function mapWebRoutes(Router $router)
{
    $router->group([
        'namespace' => $this->namespace,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');
    });
}

要验证结果,请在终端上运行PHP artisan route:list并检查路由中间件.

例如:

Now I have some route without web middleware which is defined in different file which later called in RouteServiceProvider

现在我有一些没有Web中间件的路由,它在不同的文件中定义,稍后在RouteServiceProvider中调用

要么

如果您更喜欢旧功能,可以使用以下内容

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

protected function mapGeneralRoutes(Router $router)
{
    $router->group(['namespace' => $this->namespace],function ($router) {
        require app_path('Http/routes-general.PHP');
    });
}

然后,在routes-general.PHP中,您可以像以前一样为不同的路由集创建多个中间件组

猜你在找的Laravel相关文章