如何在刀片中打印路线列表

发布于 2025-01-27 05:39:57 字数 235 浏览 5 评论 0原文

我想知道可以在刀片中打印所有路由列表,就像我们在键入php手工艺路线后在终端看到它们的方式:list

因此在这种情况下,我们只需要带有这些标头的表:

Domain

Method

URI

Middleware

Name

Action

但我并不是要将所有路由数据插入表中,然后获得结果。

我想知道我们可以自动打印所需的信息吗?

I wonder is it possible to print all route lists in a Blade, just like the way we see them in terminal after typing php artisan route:list

So in that case we just need a table with these headers:

Domain

Method

URI

Middleware

Name

Action

But I don't mean inserting all the route data in a table and then get results.

I wonder can we print the required information automatically or not?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

话少情深 2025-02-03 05:39:57

\ Artisan :: Call('oute:list');将不起作用,因为它编写以在CLI中打印路由列表,并在控制器中返回0

protected $router;

public function __construct(\Illuminate\Routing\Router $router)
{
    $this->router = $router;
}

public function index()
{
    $routes = collect($this->router->getRoutes())->map(function ($route) {
        return $this->getRouteInformation($route);
    })->filter()->all();
    dd($routes);
}

protected function getRouteInformation(\Illuminate\Routing\Route $route)
{
    return [
        'domain' => $route->domain(),
        'method' => implode('|', $route->methods()),
        'uri' => $route->uri(),
        'name' => $route->getName(),
        'action' => ltrim($route->getActionName(), '\\'),
        'middleware' => $this->get_middleware($route),
        'vendor' => $this->isVendorRoute($route),
    ];
}

protected function isVendorRoute(\Illuminate\Routing\Route $route)
{
    if ($route->action['uses'] instanceof \Closure) {
        $path = (new \ReflectionFunction($route->action['uses']))->getFileName();
    } elseif (is_string($route->action['uses']) && str_contains($route->action['uses'], 'SerializableClosure')) {
        return false;
    } elseif (is_string($route->action['uses'])) {
        if ($this->isFrameworkController($route)) {
            return false;
        }
        $path = (new \ReflectionClass($route->getControllerClass()))->getFileName();
    } else {
        return false;
    }

    return str_starts_with($path, base_path('vendor'));
}

protected function isFrameworkController(\Illuminate\Routing\Route $route)
{
    return in_array($route->getControllerClass(), [
        '\Illuminate\Routing\RedirectController',
        '\Illuminate\Routing\ViewController',
    ], true);
}

protected function get_middleware($route)
{
    return collect($this->router->gatherRouteMiddleware($route))->map(function ($middleware) {
        return $middleware instanceof \Closure ? 'Closure' : $middleware;
    })->implode("\n");
}

您可以在/vendor/laravel/framework/src/illuminate/foundation/console/routelistcommand.php file中找到此代码。

\Artisan::call('route:list'); will not work, as it is written to print the routes list in cli and return 0 in controller.

protected $router;

public function __construct(\Illuminate\Routing\Router $router)
{
    $this->router = $router;
}

public function index()
{
    $routes = collect($this->router->getRoutes())->map(function ($route) {
        return $this->getRouteInformation($route);
    })->filter()->all();
    dd($routes);
}

protected function getRouteInformation(\Illuminate\Routing\Route $route)
{
    return [
        'domain' => $route->domain(),
        'method' => implode('|', $route->methods()),
        'uri' => $route->uri(),
        'name' => $route->getName(),
        'action' => ltrim($route->getActionName(), '\\'),
        'middleware' => $this->get_middleware($route),
        'vendor' => $this->isVendorRoute($route),
    ];
}

protected function isVendorRoute(\Illuminate\Routing\Route $route)
{
    if ($route->action['uses'] instanceof \Closure) {
        $path = (new \ReflectionFunction($route->action['uses']))->getFileName();
    } elseif (is_string($route->action['uses']) && str_contains($route->action['uses'], 'SerializableClosure')) {
        return false;
    } elseif (is_string($route->action['uses'])) {
        if ($this->isFrameworkController($route)) {
            return false;
        }
        $path = (new \ReflectionClass($route->getControllerClass()))->getFileName();
    } else {
        return false;
    }

    return str_starts_with($path, base_path('vendor'));
}

protected function isFrameworkController(\Illuminate\Routing\Route $route)
{
    return in_array($route->getControllerClass(), [
        '\Illuminate\Routing\RedirectController',
        '\Illuminate\Routing\ViewController',
    ], true);
}

protected function get_middleware($route)
{
    return collect($this->router->gatherRouteMiddleware($route))->map(function ($middleware) {
        return $middleware instanceof \Closure ? 'Closure' : $middleware;
    })->implode("\n");
}

You can find this code with more information in /vendor/laravel/framework/src/Illuminate/Foundation/Console/RouteListCommand.php file.

我们的影子 2025-02-03 05:39:57

在您的控制器中,您可以使用Artisan立面获取路由列表。

public function showRoutes($request) {
    $routes = Artisan::call('route:list');
    return view('your_view', compact('routes'));  
}

In your controller you can get the list of routes using Artisan facade.

public function showRoutes($request) {
    $routes = Artisan::call('route:list');
    return view('your_view', compact('routes'));  
}
茶底世界 2025-02-03 05:39:57
@php  
$routeCollection = Route::getRoutes();
@endphp
<table>
    <tr>
        <th>HTTP Method</th>
        <th>Route</th>
        <th>Name</th>
        <th>Corresponding Action</th>
    </tr>
</table>
@foreach ($routeCollection as $route)
    <tr>
    <td> {{$value->getMethods()[0] }}</td>
    <td> {{$value->getPath() }}</td>
    <td> {{$value->getName() }}</td>
    <td> {{$value->getActionName() }}</td>
    <tr>
@endforeach

将此代码添加到刀片文件中

@php  
$routeCollection = Route::getRoutes();
@endphp
<table>
    <tr>
        <th>HTTP Method</th>
        <th>Route</th>
        <th>Name</th>
        <th>Corresponding Action</th>
    </tr>
</table>
@foreach ($routeCollection as $route)
    <tr>
    <td> {{$value->getMethods()[0] }}</td>
    <td> {{$value->getPath() }}</td>
    <td> {{$value->getName() }}</td>
    <td> {{$value->getActionName() }}</td>
    <tr>
@endforeach

on adding this code to the blade file

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文