获取 Laravel 5 中所有控制器和动作的列表
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/30596825/
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
Get list of all controllers and action in laravel 5
提问by Deejay
I am new to laravel and would like to fetch list of all controllers and list of action in that controller. Just want to know if there is a way to get a list of all Controllers as well as all their Methods by code?
我是 laravel 的新手,想获取所有控制器的列表和该控制器中的动作列表。只想知道是否有办法通过代码获取所有控制器及其所有方法的列表?
Thanks, DJ
谢谢,DJ
回答by Bogdan
By how you are explaining the need for you to know the controller actions, it seems that the actions are already mapped to routes, which means you can use the routes to get the list of mapped controllers and actions. The following code will generate an array of the registered route controller actions:
通过你如何解释你需要知道控制器动作,动作似乎已经映射到路由,这意味着你可以使用路由来获取映射控制器和动作的列表。以下代码将生成已注册的路由控制器操作的数组:
$controllers = [];
foreach (Route::getRoutes()->getRoutes() as $route)
{
$action = $route->getAction();
if (array_key_exists('controller', $action))
{
// You can also use explode('@', $action['controller']); here
// to separate the class name from the method
$controllers[] = $action['controller'];
}
}
This will ignore routes that have Closures mapped, which you don't need. Mind you, you might need to filter out any matches from routes registered by third party packages.
这将忽略您不需要的映射了闭包的路由。请注意,您可能需要从第三方包注册的路由中过滤掉任何匹配项。
回答by jalmatari
Old code:
旧代码:
public static function Controllers()
{
$controllers = require_once base_path('vendor/composer/autoload_classmap.php');
$controllers = array_keys($controllers);
$controllers = array_filter($controllers, function ($controller) {
return strpos($controller, 'App\Http\Controllers') !== false;
});
$controllers = array_map(function ($controller) {
return str_replace('App\Http\Controllers\', '', $controller);
}, $controllers);
return $controllers;
}
public static function Controllers()
{
$controllers = require_once base_path('vendor/composer/autoload_classmap.php');
$controllers = array_keys($controllers);
$controllers = array_filter($controllers, function ($controller) {
return strpos($controller, 'App\Http\Controllers') !== false;
});
$controllers = array_map(function ($controller) {
return str_replace('App\Http\Controllers\', '', $controller);
}, $controllers);
return $controllers;
}
Edited:
编辑:
This Code is much better:
这段代码要好得多:
public static function Controllers()
{
$classes = array_filter(get_declared_classes(), function ($class) {
$isController = substr($class, -10) == 'Controller';
$isNotPlainController = $isController && substr($class, -11) != '\Controller';
return $isNotPlainController;
});
//Optional: to clear controller name from its namespace
$controllers=array_map(function ($controller){
return last(explode('\',$controller));
},$classes);
//Optional: to reset keys of array to start from 0,1,2,...etc
$controllers = array_values($controllers);
return $controllers;
}
回答by Umanda
Try this
尝试这个
$controllers = [];
$i=0;
foreach (Route::getRoutes()->getRoutes() as $route)
{
$action = $route->getAction();
if (array_key_exists('controller', $action))
{
$_action = explode('@',$action['controller']);
$_namespaces_chunks = explode('\',$_action[0]);
$controllers[$i]['controller'] = end($_namespaces_chunks);
$controllers[$i]['action'] = end($_action);
$controllers[$i]['namespace'] = $action['controller'];
$controllers[$i]['route'] = $route;
}
$i++;
}
dump($controllers);
回答by MaGnetas
Try running this:
尝试运行这个:
$classes = get_declared_classes();
foreach ($classes as $class) {
if (is_subclass_of($class, 'App\Http\Controllers\Controller')) {
echo $class . '<br />';
$methods = get_class_methods($class);
foreach ($methods as $method)
echo '--- ' . $method . '<br />';
}
}
It should output all your controllers (anything inherited from the Controller class) and their methods.
它应该输出所有的控制器(从 Controller 类继承的任何东西)及其方法。
I'm not sure how to tell which ones of the methods are actual controller actions though. If you use controller routes that would be the ones starting with "get", "post" or "any", but if you're using direct links that's not the case.
不过,我不确定如何判断哪些方法是实际的控制器操作。如果您使用以“get”、“post”或“any”开头的控制器路由,但如果您使用的是直接链接,则情况并非如此。