You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

寻求可生成Laravel项目路由-控制器-模型-视图概览的工具/包

Laravel项目路由-控制器-模型-视图关联概览方案

一、自定义Artisan命令(精准匹配需求)

这种方式可以完全按照你想要的格式生成关联内容,步骤如下:

  1. 创建自定义命令
php artisan make:command RouteRelationshipReport
  1. 编写命令逻辑
    打开生成的app/Console/Commands/RouteRelationshipReport.php,替换内容为:
<?php

namespace App\Console\Commands;

use Illuminate\Console\Command;
use Illuminate\Support\Facades\Route;
use ReflectionClass;
use ReflectionMethod;

class RouteRelationshipReport extends Command
{
    protected $signature = 'route:relationship-report';
    protected $description = 'Generate a report of route -> controller@method -> models -> view';

    public function handle()
    {
        // 打印表头
        $this->line(str_pad('route', 20) . ' -> ' . str_pad('controller@function', 30) . ' -> ' . str_pad('models', 20) . ' -> ' . 'view');
        $this->line(str_repeat('-', 100));

        // 遍历所有路由
        foreach (Route::getRoutes() as $route) {
            // 跳过匿名路由和无控制器的路由
            if (!$route->getAction('controller')) {
                continue;
            }

            [$controllerClass, $method] = explode('@', $route->getAction('controller'));
            $routeUri = $route->uri();

            // 获取方法用到的模型
            $models = $this->getUsedModels($controllerClass, $method);
            $modelsStr = implode(', ', $models);

            // 获取返回的视图
            $view = $this->getReturnedView($controllerClass, $method);

            // 格式化输出
            $this->line(
                str_pad($routeUri, 20) . ' -> ' .
                str_pad(class_basename($controllerClass)."@$method", 30) . ' -> ' .
                str_pad($modelsStr, 20) . ' -> ' .
                ($view ?? 'N/A')
            );
        }
    }

    // 获取控制器方法中用到的模型
    protected function getUsedModels(string $controllerClass, string $method): array
    {
        if (!class_exists($controllerClass)) {
            return [];
        }

        $reflection = new ReflectionMethod($controllerClass, $method);
        $parameters = $reflection->getParameters();
        $models = [];

        // 解析方法参数中的模型
        foreach ($parameters as $param) {
            $type = $param->getType();
            if ($type && !$type->isBuiltin() && class_exists($type->getName())) {
                $className = $type->getName();
                if (is_subclass_of($className, \Illuminate\Database\Eloquent\Model::class)) {
                    $models[] = class_basename($className);
                }
            }
        }

        // 解析方法体中查询的模型(简单实现)
        $methodBody = file_get_contents($reflection->getFileName());
        $methodBody = substr($methodBody, $reflection->getStartLine(), $reflection->getEndLine() - $reflection->getStartLine());
        preg_match_all('/(\w+)::/', $methodBody, $matches);
        foreach ($matches[1] as $className) {
            $fullClass = 'App\\Models\\' . $className;
            if (class_exists($fullClass) && is_subclass_of($fullClass, \Illuminate\Database\Eloquent\Model::class)) {
                $models[] = $className;
            }
        }

        return array_unique($models);
    }

    // 获取控制器方法返回的视图
    protected function getReturnedView(string $controllerClass, string $method): ?string
    {
        if (!class_exists($controllerClass)) {
            return null;
        }

        $reflection = new ReflectionMethod($controllerClass, $method);
        $methodBody = file_get_contents($reflection->getFileName());
        $methodBody = substr($methodBody, $reflection->getStartLine(), $reflection->getEndLine() - $reflection->getStartLine());

        // 匹配return view('xxx')格式
        if (preg_match('/return\s+view\(\'([^\']+)\'/', $methodBody, $matches)) {
            return $matches[1];
        }

        // 匹配return view("xxx")格式
        if (preg_match('/return\s+view\("([^"]+)"/', $methodBody, $matches)) {
            return $matches[1];
        }

        return null;
    }
}
  1. 运行命令生成报告
php artisan route:relationship-report

该命令会输出你需要的关联格式内容,包含路由、控制器方法、用到的模型和返回的视图。

二、扩展route:list命令的工具

如果不想自定义命令,可以通过Composer安装第三方增强包,这类包大多能在原生route:list基础上补充模型、视图关联信息,安装后运行扩展后的路由命令,再自行整理成目标格式即可。

三、辅助定位工具:Laravel IDE Helper

它不直接生成关联报告,但能生成路由、控制器、模型的注释信息,在IDE中可快速跳转关联文件,减少定位时间。安装后运行以下命令生成辅助文件:

php artisan ide-helper:models
php artisan ide-helper:routes

内容的提问来源于stack exchange,提问作者Mani

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.21 06:43:27