Php 是否有任何内置方法用于获取具有函数名的所有控制器列表?
是否有任何内置方法可以获取控制器列表及其函数名,如下所示:Php 是否有任何内置方法用于获取具有函数名的所有控制器列表?,php,laravel,Php,Laravel,是否有任何内置方法可以获取控制器列表及其函数名,如下所示: $routes = array( 'contoller1' => array('index','delete','store'), 'contoller2' => array('index','delete','show'), 'contoller3' => array('show','insertData','delet
$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())代码>
但是当您键入php artisan routes
或php artisan route:list
时,它返回一个包含大量信息的非常大的数组,根据框架版本,它会获得所有路由和关联控制器的列表
因此,如果你进入源代码,你可以看到确切地如何得到你想要的
第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);
你可以使用then和
在laravel中,每个控制器都有单独的文件,就像codeigniter?我需要一个二维数组,在系统中有控制器名和函数名。我实际上需要二维数组,其中键是控制器名,值数组包含函数名。@PayerAhammed-请查看更新的答案。它是完全未经测试的,并且是动态编写的,因此可能会包含bug,所以不要只是复制粘贴说它不工作!希望它能为您指明正确的方向。据说explode()希望参数2是字符串,数组给定我实际上需要二维数组,其中key是控制器名,value数组包含函数名。我所做的一切,谢谢@whoacowboy。这意味着我必须用上面的代码来完成。没有内置的方法可以找到这个?@PayerAhammed-没有内置的方法,因为你有一个非常特殊的用例,几乎没有人需要这样做。此外,为什么会有一种特定的方法来完全按照您想要的方式格式化阵列中的路由?
// 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);