laravel 分割多个路由文件

laravel框架将web.php拆分为多个路由文件
第一种方法:
使用inclue_once将创建的其他路由文件引入到web.php文件中
在/routes目录下创建test.php路由文件
在web.php使用include_once引入路由文件

include_once '../routes/test.php';

第二种方法:
查找laravel框架加载web.php的方式,仿照web.php的加载凡是添加新的路由文件
在/routes目录下创建test.php路由文件
找到app\Providers\RouteServiceProvider.php文件的boot()方法,仿照web.php方式,添加加载新的路由文件

$this->routes(function () {
     Route::prefix('api')
         ->middleware('api')
         ->namespace($this->namespace)
         ->group(base_path('routes/api.php'));

     Route::middleware('web')
         ->namespace($this->namespace)
         ->group(base_path('routes/web.php'));

     Route::middleware('test')
         ->namespace($this->namespace)
         ->group(base_path('routes/test.php'));
 });

middleware()方法是用来加载中间件的,可以为路由文件添加独立的中间件,也可以直接使用web的中间件。添加方式就仿照web的中间件
在App\Http\Kernel.php找到web的中间件,复制web的中间件,并改名为test

protected $middlewareGroups = [
        'web' => [
            \App\Http\Middleware\EncryptCookies::class,
            \Illuminate\Cookie\Middleware\AddQueuedCookiesToResponse::class,
            \Illuminate\Session\Middleware\StartSession::class,
            // \Illuminate\Session\Middleware\AuthenticateSession::class,
            \Illuminate\View\Middleware\ShareErrorsFromSession::class,
            \App\Http\Middleware\VerifyCsrfToken::class,
            \Illuminate\Routing\Middleware\SubstituteBindings::class,
        ],

        'api' => [
            'throttle:api',
            \Illuminate\Routing\Middleware\SubstituteBindings::class,
        ],

        'test' => [
            \App\Http\Middleware\EncryptCookies::class,
            \Illuminate\Cookie\Middleware\AddQueuedCookiesToResponse::class,
            \Illuminate\Session\Middleware\StartSession::class,
            // \Illuminate\Session\Middleware\AuthenticateSession::class,
            \Illuminate\View\Middleware\ShareErrorsFromSession::class,
            \App\Http\Middleware\VerifyCsrfToken::class,
            \Illuminate\Routing\Middleware\SubstituteBindings::class,
        ],
    ];

版权声明:本文为weixin_44245595原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接和本声明。