加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 大数据 > 正文

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

发布时间:2020-12-14 19:35:04 所属栏目:大数据 来源:网络整理
导读:在此 changes即Laravel 5.2.31及更高版本之后,app / Http / routes.php中的所有路由都将属于Web中间件组. 在RouteServiceProvider.php中 protected function mapWebRoutes(Router $router){ $router-group([ 'namespace' = $this-namespace,'middleware' = '
在此 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中,您可以像以前一样为不同的路由集创建多个中间件组

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读