在此
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并检查路由中间件.
例如:
现在我有一些没有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中,您可以像以前一样为不同的路由集创建多个中间件组