How to get a list of registered route paths in Lar

2019-01-10 20:17发布

I was hoping to find a way to create an array with the registered routes paths within Laravel 4.

Essentially, I am looking to get a list something like this returned:

/
/login
/join
/password

I did come across a method Route::getRoutes() which returns an object with the routes information as well as the resources but the path information is protected and I don't have direct access to the information.

Is there any other way to achieve this? Perhaps a different method?

11条回答
啃猪蹄的小仙女
2楼-- · 2019-01-10 20:37
     $routeList = Route::getRoutes();

    foreach ($routeList as $value)
    {
        echo $value->uri().'<br>';
    }

use Illuminate\Support\Facades\Route;

On Laravel 5.4, it works, 100 %

查看更多
等我变得足够好
3楼-- · 2019-01-10 20:39

if you have compiled routes like /login/{id} and you want prefix only:

foreach (Route::getRoutes() as $route) {
    $compiled = $route->getCompiled();
    if(!is_null($compiled))
    {
        var_dump($compiled->getStaticPrefix());
    }
}
查看更多
等我变得足够好
4楼-- · 2019-01-10 20:40

I created a route that will list each route and its respective details in an html table.

Route::get('routes', function() {
    $routeCollection = Route::getRoutes();

    echo "<table style='width:100%'>";
        echo "<tr>";
            echo "<td width='10%'><h4>HTTP Method</h4></td>";
            echo "<td width='10%'><h4>Route</h4></td>";
            echo "<td width='10%'><h4>Name</h4></td>";
            echo "<td width='70%'><h4>Corresponding Action</h4></td>";
        echo "</tr>";
        foreach ($routeCollection as $value) {
            echo "<tr>";
                echo "<td>" . $value->getMethods()[0] . "</td>";
                echo "<td>" . $value->getPath() . "</td>";
                echo "<td>" . $value->getName() . "</td>";
                echo "<td>" . $value->getActionName() . "</td>";
            echo "</tr>";
        }
    echo "</table>";
});
查看更多
别忘想泡老子
5楼-- · 2019-01-10 20:44

You can use console command:

php artisan routes

Helpers :

Usage:
 routes [--name[="..."]] [--path[="..."]]

Options:
 --name                Filter the routes by name.
 --path                Filter the routes by path.
 --help (-h)           Display this help message.
 --quiet (-q)          Do not output any message.
 --verbose (-v|vv|vvv) Increase the verbosity of messages: 1 for normal output, 2 for     more verbose output and 3 for debug
 --version (-V)        Display this application version.
 --ansi                Force ANSI output.
 --no-ansi             Disable ANSI output.
 --no-interaction (-n) Do not ask any interactive question.
 --env                 The environment the command should run under.
查看更多
相关推荐>>
6楼-- · 2019-01-10 20:45

For Laravel 5, you can use artisan command

php artisan route:list instead of php artisan routes.

查看更多
劫难
7楼-- · 2019-01-10 20:46

Route::getRoutes() returns a RouteCollection. On each element, you can do a simple $route->getPath() to get path of the current route.

Each protected parameter can be get with a standard getter.

Looping works like this:

$routeCollection = Route::getRoutes();

foreach ($routeCollection as $value) {
    echo $value->getPath();
}
查看更多
登录 后发表回答