Laravel 在视图中列出路由

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/43537706/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-14 15:50:02  来源:igfitidea点击:

Laravel list routes in the view

phplaravelviewrouteslaravel-5.4

提问by Micheasl

How can I list routes specifically the api routes in my view?

如何在我的视图中专门列出 api 路由?

For example These:

例如这些:

...api/user
...api/Information

The artisan command for example does list them like this:

例如, artisan 命令确实会像这样列出它们:

php artisan route:list

回答by Pankit Gami

In your controller you can get the list of routes using Artisanfacade. I assume all your apiroutes have apistring in its path.:

在您的控制器中,您可以使用ArtisanFacade获取路由列表。我假设您的所有api路线api在其路径中都有字符串。:

public function showRoutes($request) {
    $routes = Artisan::call('route:list', ['--path' => 'api']);
    return view('your_view', compact('routes'));  
}

Edit :

编辑 :

You can also use Routefacades getRoutesmethod.

您也可以使用RouteFacadesgetRoutes方法。

$routes = [];
foreach (\Route::getRoutes()->getIterator() as $route){
    if (strpos($route->uri, 'api') !== false){
        $routes[] = $route->uri;
    }
}
return view('your_view', compact('routes'));