ThinkPHP5.0自定义路由检测路径问题诊断脚本
2025-11-03 / PHP / 85 次围观 / 0 次吐槽 /功能说明
自动检测当前 ThinkPHP 版本
检查
application/config.php中是否正确设置'route_config_file'检查
/application/route.php是否存在、可读检查文件是否被加载执行
打印系统已注册路由(最多 30 条)
判断
test1是否存在输出详细诊断结果和修复建议
保存路径
文件名:
/application/command/CheckRouteSmart.php
完整代码
<?php
namespace app\command;
use think\console\Command;
use think\console\Input;
use think\console\Output;
class CheckRouteSmart extends Command
{
protected function configure()
{
$this->setName('check:route-smart')
->setDescription('ThinkPHP 5.0 路由检测 + 路径问题诊断');
}
protected function execute(Input $input, Output $output)
{
$output->writeln("========== ThinkPHP 路由诊断工具 ==========\n");
// 检查框架版本
$versionFile = THINK_PATH . 'base.php';
$version = '未知';
if (file_exists($versionFile)) {
$content = file_get_contents($versionFile);
if (preg_match('/define\([\'"]THINK_VERSION[\'"],\s*[\'"](.+?)[\'"]\)/', $content, $m)) {
$version = $m[1];
}
}
$output->writeln(" 检测到 ThinkPHP 版本: {$version}\n");
// 检查路由配置文件路径
$configFile = APP_PATH . 'config.php';
if (!file_exists($configFile)) {
$output->writeln("未找到配置文件: {$configFile}");
return;
}
$config = include $configFile;
$routeFiles = isset($config['route_config_file']) ? $config['route_config_file'] : ['route'];
$output->writeln(" 当前配置的路由文件: " . json_encode($routeFiles) . "\n");
// 检查路由文件是否存在
$foundFiles = [];
foreach ($routeFiles as $name) {
$path = APP_PATH . $name . '.php';
if (file_exists($path)) {
$foundFiles[] = $path;
$output->writeln("找到路由文件: {$path}");
} else {
$output->writeln(" 未找到路由文件: {$path}");
}
}
if (empty($foundFiles)) {
$output->writeln("\n没有任何路由文件存在,请在 application 目录创建 route.php。");
return;
}
// 检查路由文件是否被加载
$output->writeln("\n 检查路由文件是否被加载...");
$markerFile = RUNTIME_PATH . 'route_marker.txt';
@unlink($markerFile);
foreach ($foundFiles as $f) {
$append = "\nif (!file_exists('{$markerFile}')) file_put_contents('{$markerFile}', basename(__FILE__), FILE_APPEND);";
$content = file_get_contents($f);
if (strpos($content, 'route_marker.txt') === false) {
file_put_contents($f, $append, FILE_APPEND);
}
// 直接 include 路由文件以触发加载,不加载 index.php
include_once $f;
}
sleep(1);
if (file_exists($markerFile)) {
$output->writeln("route.php 已被执行加载。");
unlink($markerFile);
} else {
$output->writeln("route.php 未执行,可能路径或配置错误。");
$output->writeln("请检查 config.php 中的 'route_config_file' 设置是否为 ['route']。\n");
}
// 获取当前所有注册路由
$output->writeln("\n当前已注册的路由(最多显示30条):");
$ref = new \ReflectionClass('think\\Route');
$routes = [];
foreach (['rules', 'name'] as $propName) {
if ($ref->hasProperty($propName)) {
$p = $ref->getProperty($propName);
$p->setAccessible(true);
$routes[$propName] = $p->getValue();
}
}
$count = 0;
foreach ($routes['rules'] as $method => $list) {
foreach ($list as $rule => $value) {
$target = is_array($value) && isset($value['route']) ? $value['route'] : json_encode($value);
$output->writeln(sprintf(" [%s] %s => %s", strtoupper($method), $rule, $target));
if (++$count >= 30) {
$output->writeln(" ... (仅显示前30条)\n");
break 2;
}
}
}
// 检查 test1 是否存在
$target = 'test1';
$exists = false;
foreach ($routes['rules'] as $method => $rules) {
if (isset($rules[$target])) {
$exists = true;
break;
}
}
$output->writeln("\n========== 检测结果 ==========");
if ($exists) {
$output->writeln("路由 '{$target}' 已定义,对应控制器:tool/test1");
} else {
$output->writeln("未检测到路由 '{$target}'。");
$output->writeln("请检查 route.php 是否存在以下定义:");
$output->writeln("Route::get(['test1' => 'tool/test1']);");
}
$output->writeln("\n检测完成。\n");
}
}注册命令
在 /application/command.php 中添加:
<?php return [ 'app\command\CheckRouteSmart', ];
使用方法
php think check:route-smart
- 上一篇:Linux 删除指定网卡路由
- 下一篇:debian安装mysql 5.7
Powered By Cheug's Blog
Copyright Cheug Rights Reserved.