是否有任何内置方法可以获取控制器列表及其函数名称,如下所示:
$routes = array(
'contoller1' => array('index','delete','store'),
'contoller2' => array('index','delete','show'),
'contoller3' => array('show','insertData','delete'),
......
..
);
也许可以从Route :: getRoutes() - > getRoutes()中找到控制器。
var_dump(Route::getRoutes()->getRoutes());
但它会返回一个包含大量信息的非常大的数组
答案 0 :(得分:1)
当您键入php artisan routes
或php artisan route:list
时,根据框架版本,它会获取所有路由和关联控制器的列表。
因此,如果你进入源代码,你就可以看到如何获得你想要的东西。
https://github.com/laravel/framework/blob/4.2/src/Illuminate/Foundation/Console/RoutesCommand.php
第82至112行显示如何将路径编译为可显示的格式。
从源代码中无耻地进行了讽刺,以供参考。
/**
* Compile the routes into a displayable format.
*
* @return array
*/
protected function getRoutes()
{
$results = array();
foreach ($this->routes as $route)
{
$results[] = $this->getRouteInformation($route);
}
return array_filter($results);
}
/**
* Get the route information for a given route.
*
* @param \Illuminate\Routing\Route $route
* @return array
*/
protected function getRouteInformation(Route $route)
{
$uri = implode('|', $route->methods()).' '.$route->uri();
return $this->filterRoute(array(
'host' => $route->domain(),
'uri' => $uri,
'name' => $route->getName(),
'action' => $route->getActionName(),
'before' => $this->getBeforeFilters($route),
'after' => $this->getAfterFilters($route)
));
}
你可能只想迭代ang获取动作名称。 $route->getActionName();
或者简单的方法:
$routes = app()['router']->getRoutes();
$controllers = [];
foreach ($routes as $route) {
$controllers[] = $route->getAction();
}
$collection = [];
foreach ($controllers as $c) {
explode ( "@" , $c, 1 )
if (!isset($collection[$c[0]])) {
$collection[$c[0]] = [];
}
$collection[$c[0]][] = $c[1];
}
dd($collection);
答案 1 :(得分:1)
您可以使用getRoutes
然后getPath
和getAction
// Get a collection of all the routes
$routeCollection = Route::getRoutes();
// Create your base array of routes
$routes = [];
// loop through the collection of routes
foreach ($routeCollection as $route) {
// get the action which is an array of items
$action = $route->getAction();
// if the action has the key 'controller'
if (array_key_exists('controller', $action)) {
// explode the string with @ creating an array with a count of 2
$explodedAction = explode('@', $action['controller']);
// check to see if an array exists for the controller name
if (!isset($routes[$explodedAction[0]])) {
// if not create it, this will look like
// $routes['controllerName']
$routes[$explodedAction[0]] = [];
}
// set the add the method name to the controller array
$routes[$explodedAction[0]][] = $explodedAction[1];
}
}
// show the glory of your work
dd($routes);