How to Get All Controller with Action List in Laravel

Hello Artisan,

In this quick example tutorial, I will show you how to get all controllers with action lists in laravel 8 application. I will show you that we can get all the route list with our action which means methods.

So if you don't know how to do it, then this quick example is for you. Sometimes we need to show or get all the controller lists in our application. This example is completely for that situation. 


use Illuminate\Support\Facades\Route;
use App\Http\Controllers\CheckController;
use App\Http\Controllers\HomeController;

Auth::routes();
Route::get('/home', [HomeController::class, 'index'])->name('home');

Route::get('/',function() {

    $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'];
        }
    }

    return $controllers;
});

 

Now if you check, you will see the below ourput. 

[
    "Facade\\Ignition\\Http\\Controllers\\HealthCheckController",
    "Facade\\Ignition\\Http\\Controllers\\ExecuteSolutionController",
    "Facade\\Ignition\\Http\\Controllers\\ShareReportController",
    "Facade\\Ignition\\Http\\Controllers\\ScriptController",
    "Facade\\Ignition\\Http\\Controllers\\StyleController",
    "Laravel\\Sanctum\\Http\\Controllers\\CsrfCookieController@show",
    "App\\Http\\Controllers\\Auth\\LoginController@showLoginForm",
    "App\\Http\\Controllers\\Auth\\LoginController@login",
    "App\\Http\\Controllers\\Auth\\LoginController@logout",
    "App\\Http\\Controllers\\Auth\\RegisterController@showRegistrationForm",
    "App\\Http\\Controllers\\Auth\\RegisterController@register",
    "App\\Http\\Controllers\\Auth\\ForgotPasswordController@showLinkRequestForm",
    "App\\Http\\Controllers\\Auth\\ForgotPasswordController@sendResetLinkEmail",
    "App\\Http\\Controllers\\Auth\\ResetPasswordController@showResetForm",
    "App\\Http\\Controllers\\Auth\\ResetPasswordController@reset",
    "App\\Http\\Controllers\\Auth\\ConfirmPasswordController@showConfirmForm",
    "App\\Http\\Controllers\\Auth\\ConfirmPasswordController@confirm",
    "App\\Http\\Controllers\\HomeController@index"
]

 

Recommended: Laravel Spatie Roles and Permissions Tutorial from Scratch

 

Hope it can help you.

 

Facebook Github
A web enthusiastic, a self-motivated full-stack software engineer from Dhaka, Bangladesh with experience in developing applications using Laravel , React and Vue js