在Laravel> = 5.2.31的路由中避免/删除Web中间件 [英] Avoid/remove web middleware in routes for Laravel >= 5.2.31

查看:123
本文介绍了在Laravel> = 5.2.31的路由中避免/删除Web中间件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

更改(即Laravel 5.2.31及更高版本)之后,app/Http/routes.php将属于Web中间件组.

After this changes which is Laravel 5.2.31 and above, all routes in app/Http/routes.php will fall under web middleware group.

RouteServiceProvider.php

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

问题:

  1. 在没有web中间件的情况下定义路由集合的最简单/最佳方法是什么?
  1. What is the easiest/best way to define set of routes without web middleware?

一种使用情况是,声明不带会话中间件的无状态api的路由,该路由不属于Web组中间件

One of the use case for this is, declaring routes for stateless api without session middleware which falls under web group middleware

推荐答案

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

One way I solved this is by editing the app/Providers/RouteServiceProvider.php and having another route files for other group middleware ie api

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

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

To verify the result, run php artisan route:list on terminal and check the route middleware.

例如:

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

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

OR

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

If you prefer the old features, you can have something like this:

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中,您可以像以前一样为不同的路由集设置多个中间件组

Then, in routes-general.php you can have multiple middleware groups for different set of routes just like before

这篇关于在Laravel> = 5.2.31的路由中避免/删除Web中间件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆