针对hyperf框架改造----路由(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 // 用户路由
  • 致此,大功告成
本作品采用《CC 协议》,转载必须注明作者和本文链接
本帖由系统于 2年前 自动加精
codezhao
讨论数量: 3

不错

2年前 评论
codezhao (楼主) 2年前
<?php

declare(strict_types=1);

$files = glob(BASE_PATH . '/routes/{admin,front}/*.php', GLOB_BRACE);

foreach ($files as $file) require_once $file;
1年前 评论

讨论应以学习和精进为目的。请勿发布不友善或者负能量的内容,与人为善,比聪明更重要!