是否有任何内置方法可以获取所有带有函数名称的控制器列表?
Is there any built-in method for getting all controller list with function name?
是否有任何内置方法可以获取控制器列表及其函数名称如下:
$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());
但它 returns 一个包含大量信息的非常大的数组
当您键入 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)
));
}
您可能只想迭代并获取操作名称。 $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);
您可以使用getRoutes
then getPath
and 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);
是否有任何内置方法可以获取控制器列表及其函数名称如下:
$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());
但它 returns 一个包含大量信息的非常大的数组
当您键入 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)
));
}
您可能只想迭代并获取操作名称。 $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);
您可以使用getRoutes
then getPath
and 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);