当你使用larvel创建一个相对比较复杂的web网站时,往往你的routes文件就会变得很庞大,我们需要的定义的规则越来越多,如果几百上千个路由都定义在一个文件中,如何去维护?,可以按照功能模块来划分,说一种比较实用的方法:
项目\app\Providers\RouteServiceProvider.php
把你定义的路由方法添加到map
public function map()
{
$this->mapApiRoutes();$this->mapWebRoutes();//这里注册自己想要的 路由方法$this->map_device_WebRoutes();}
添加路由方法
protected function map_device_WebRoutes(){
Route::prefix('device')->middleware('device/web')->namespace($this->namespace)->group(base_path('routes/device/web.php'));}
然后去中间件添加路由和需要的中间件添加
项目\app\Http\kernel.php
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:60,1','bindings',],//我们添加的'device/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,],
]
在routes创建device\web.php
//创建
项目\routes\device\web.php
编辑 项目\routes\device\web.php
//写入
route::get('/',function(){
return 'ok';
});
访问地址 :域名 + /device/ 即可访问到我们的定义的路由文件