Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Get list of all controllers and action in laravel 5

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

like image 446
Daljeet Singh Avatar asked Jun 02 '15 12:06

Daljeet Singh


3 Answers

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.

like image 61
Bogdan Avatar answered Oct 24 '22 10:10

Bogdan


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.

like image 2
Tushar Saha Avatar answered Oct 24 '22 10:10

Tushar Saha


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;
}
like image 1
jalmatari Avatar answered Oct 24 '22 11:10

jalmatari