Laravel中我们除了使用框架为我们自己生成的web.php或者api.php之外,我们还可以根据我们自己的业务需求自定义路由文件。
下边我们大概记录一下新增自定义路由文件的过程:
1:新建路由文件
在routes/目录下新建路由文件 routes/test.php内容如下
Route::group(['prefix' => test], function () {
Route::group(['prefix' => 'report'], function () {
Route::get(x, 'Test\ReportController@xxx);
Route::get(xx, 'Test\xxx@xxx);
});
// 测试数据库备份
Route::get("xxx", xxxx\xxxx@xxxx);
// 测试数据库还原
Route::get("xxxx", xxx\xxxxx@xxxx);
// 测试数据库备份文件删除
Route::get("xxxxx", xxxx\xxxx@xxxx);
});
2:修改RouteServiceProvider.php
app\Providers\RouterServiceProvider.php新增代码
/**
* Define the routes for the application.
*
* @return void
*/
public function map()
{
$this->mapApiRoutes();
$this->mapWebRoutes();
// 这个是我们自定义的,名字根据你自己定义的名字来
$this->mapTestRoutes();
}
再继续在这个文件下边添加下边的代码:
/**
* @name: 函数名
* @desc: 20200804启用
* @author: camellia
* @date: 20200804
*/
protected function mapTestRoutes()
{
Route::namespace($this->namespace)
->group(base_path('routes/test.php'));
}
这个就是定义路由的访问方式,我上边定义的这个就直接在index.PHP之后加你的路由名称访问就可以了。
如果你需要特定的前缀,配置可以参考:api.php的配置
/**
* Define the "api" routes for the application.
*
* These routes are typically stateless.
*
* @return void
*/
protected function mapApiRoutes()
{
Route::prefix('api')
->middleware('api')
->namespace($this->namespace)
->group(base_path('routes/api.php'));
}
原文链接:https://guanchao.site/index/article/articledetail.html?artid=JoXHprtpS
有好的建议,请在下方输入你的评论。
网友评论