laravel5.8(十五)新增自定义路由文件

30 篇文章 1 订阅
27 篇文章 1 订阅

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

有好的建议,请在下方输入你的评论。

欢迎访问个人博客
https://guanchao.site

欢迎访问小程序:
在这里插入图片描述

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值