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?
Thanks, DJ
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.
That worked for me.
Extracting all controllers (App\Http\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);
dd($controllers);
Extracting all methods in a specific controller using ReflectionClass
$namespace = "App\Http\Controllers";
$controller = "TestController";
$controller_class = new ReflectionClass($namespace.'\\'.$controller);
$controller_methods = $controller_class->getMethods(ReflectionMethod::IS_PUBLIC);
dd($controller_methods);
One catch though. You may need to run composer dump-autoload after creating a controller.
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;
}
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;
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With