如何获得Laravel的注册path列表?

我希望find一种方法来创build一个数组与Laravel 4注册的路线path。

基本上,我正在寻找一个像这样的返回列表:

/ /login /join /password 

我遇到了Route::getRoutes() ,它返回一个包含路由信息和资源的对象,但是path信息是受保护的,我没有直接访问信息。

有没有其他办法可以做到这一点? 也许是不同的方法?

Route::getRoutes()返回一个RouteCollection 。 在每个元素上,可以执行一个简单的$route->getPath()来获取当前path的path。

每个受保护的参数都可以用一个标准的getter获得。

循环播放是这样的:

 $routeCollection = Route::getRoutes(); foreach ($routeCollection as $value) { echo $value->getPath(); } 

您可以使用控制台命令:

 php artisan routes 

帮手:

 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. 

对于Laravel 5,你可以使用手工命令

php artisan route:list而不是php artisan routes

我创build了一个path,将在html表中列出每个路线及其相应的细节。

 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>"; }); 
 //Laravel 5.4 //Controller index() $app = app(); $routes = $app->routes->getRoutes(); return view ('Admin::routes.index',compact('routes')); //view <table id="routes-table" class="table table-bordered table-responsive"> <thead> <tr> <th>uri</th> <th>Name</th> <th>Type</th> <th>Method</th> </tr> </thead> <tbody> @foreach ($routes as $route ) <tr> <td>{{$route->uri}}</td> <td>{{$route->getName()}}</td> <td>{{$route->getPrefix()}}</td> <td>{{$route->getActionMethod()}}</td> </tr> @endforeach </tbody> </table> 

一个更好的方法来获得它的可读性是注册一个路线,并直接在网页浏览器与手工输出

 Route::get('routes', function() { \Artisan::call('route:list'); return \Artisan::output(); }); 

如果你已经编译了像/ login / {id}这样的路线,而你只想要前缀:

 foreach (Route::getRoutes() as $route) { $compiled = $route->getCompiled(); if(!is_null($compiled)) { var_dump($compiled->getStaticPrefix()); } } 
  $routeList = Route::getRoutes(); foreach ($routeList as $value) { echo $value->uri().'<br>'; } 

使用Illuminate \ Support \ Facades \ Route;

在Laravel 5.4上,它可以工作,100%

对于那些使用Laravel 5插件使用Oh-my-zsh的控制台命令

 la5routes