hyperf 查看所有路由的命令

hyperf的版本
"hyperf/framework": "~3.1.0",

<?php

declare(strict_types=1);

namespace App\Commands;

use Hyperf\Command\Annotation\Command;
use Hyperf\Command\Command as HyperfCommand;
use Psr\Container\ContainerInterface;
use Hyperf\Contract\ConfigInterface;
use Hyperf\HttpServer\Router\DispatcherFactory;
use Symfony\Component\Console\Command\Command as SymfonyCommand;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Output\OutputInterface;
use Symfony\Component\Console\Helper\Table;

#[Command]
class RouteListCommand extends HyperfCommand
{
    public function __construct(protected ContainerInterface $container)
    {
        parent::__construct('route:list');
    }

    protected function execute(InputInterface $input, OutputInterface $output): int
    {
        $factory = $this->container->get(DispatcherFactory::class);
        $router = $factory->getRouter('http');
        $routes = $router->getData();

        $config = $this->container->get(ConfigInterface::class);
        $server = $config->get('server.servers.0', ['host' => '0.0.0.0', 'port' => 9501]);
        $domain = "http://{$server['host']}:{$server['port']}";


        $rows = [];
        foreach ($routes as $routeGroup) {
            // POST GET
            if (is_array($routeGroup) && !empty($routeGroup)) {
                foreach ($routeGroup as $httpMethod => $routeItems) {
                    // route object
                    if (is_array($routeItems) && !empty($routeItems)) {
                        foreach ($routeItems as $url => $routeItem) {
                            $uri = $domain . $routeItem->route;
                            $rows[] = [
                                $httpMethod,
                                $uri,
                                $this->formatHandler($routeItem->callback),
                            ];
                        }
                    }
                }
            }

        }

        $table = new Table($output);
        $table->setHeaders(['Method', 'URI', 'Handler'])
            ->setRows($rows);
        $table->render();

        return SymfonyCommand::SUCCESS;
    }

    private function formatHandler($handler): string
    {
        if (is_array($handler) && isset($handler[0], $handler[1])) {
            return $handler[0] . '@' . $handler[1];
        }
        return is_string($handler) ? $handler : 'Closure';
    }
}


执行命令: php bin/hyperf.php route:list

+--------+---------------------------------------------------------+--------------------------------------------------------+
| Method | URI                                                     | Handler                                                |
+--------+---------------------------------------------------------+--------------------------------------------------------+
| POST   | http://0.0.0.0:9501/api/driver/common/driverLogin/xx    | App\API\Common\Controllers\DriverLoginController@xx    |
| POST   | http://0.0.0.0:9501/api/driver/common/tool/xx           | App\API\Common\Controllers\ToolController@xx           |
| POST   | http://0.0.0.0:9501/api/driver/common/region/xx         | App\API\Common\Controllers\ToolController@xx           |
| POST   | http://0.0.0.0:9501/api/driver/logic/driverLog/xx       | App\API\Logic\Controllers\DriverLogController@xx       |
+--------+---------------------------------------------------------+--------------------------------------------------------+

posted on 2025-05-06 13:14  zh7314  阅读(48)  评论(0)    收藏  举报