针对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 协议》,转载必须注明作者和本文链接
本帖由系统于 3年前 自动加精
不错