7.5路由(routes)多文件多目录多前缀支持

目的

  • 实现下图效果


第一步 新增助手函数

  • 添加助手函数的方法就不在这里赘述,不清楚的可以百度一下
if (! function_exists('reloadRoute')) {
    /**
     * 加载路由.
     */
    function reloadRoute()
    {
        $path = BASE_PATH . '/routes';
        $dirs = scandir($path);
        foreach ($dirs as $dir) {
            if ($dir != '.' && $dir != '..') {
                $routeFilePath = $path . "/{$dir}";
                $files = scandir($routeFilePath);
                foreach ($files as $file) {
                    if ($file != '.' && $file != '..') {
                        require_once $routeFilePath . '/' . basename($file);
                    }
                }
            }
        }
    }
}

第三步 加载路由

  • 在config目录下的routes.php中写入在家路由函数:
<?php

declare(strict_types=1);
reloadRoute();

第三步 添加routers目录

  • 在项目根(/)目录新增routes,达到效果例如:
routes
├── admin // 后台路由
│   ├── user.php // 用户接口路由
├── front // 前台
│   ├── home.php // 前台对外公共路由
│   ├── user.php // 用户路由
  • 致此,大功告成
文档更新时间: 2021-09-24 18:32   作者:赵豪