DEV Community

Yasser Elgammal
Yasser Elgammal

Posted on

How to Execute Laravel Artisan Commands Using an API Endpoint

In Laravel application deployment, sometimes we need to execute server-side commands for tasks like database migrations, cache clearance, or other essential processes.

In the traditional way:

  • Open cPanel or any Control Panel then go to the terminal or go into SSH directly if you have root access,
  • Then you have to navigate to the application folder and run the command you need.

But, These steps may take a little time.

Here's the solution:

In our Laravel Project, We will create an API Endpoint to handle this for us, So let's begin:

1- Create a Controller with any name, I will name it CommandController

php artisan make:controller Api/CommandController
Enter fullscreen mode Exit fullscreen mode

2- Then we go to Api routes and add the route to handle and reach our Endpoint:

<?php

namespace App\Http\Controllers\Api;

use App\Http\Controllers\Controller;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Artisan;

class CommandController extends Controller
{
    public function runCommand(Request $request)
    {
            $validated = $request->validate(['command' => ['required', 'string']]);

            // Run the Artisan command
            Artisan::call($validated['command']);

            // Get the output of the command
            $output = Artisan::output();

            return response()->json(['message' => 'Command executed successfully', 'output' => $output]);
    }
}
Enter fullscreen mode Exit fullscreen mode
  • Still an important step for securing this Endpoint, So how can we secure it?

Securing might be different depending on your situation.

For Example, you can use [Sanctum Token Abilities], For more information review my previous article about it Dive into Laravel Sanctum Token Abilities,

But Now, We will go through securing it using Middleware:

3- Create a Middleware, this Middleware will run this Endpoint if our App is in debug mode only...

php artisan make:middleware DebugModeOnly
Enter fullscreen mode Exit fullscreen mode

4- Here's DebugModeOnly Middleware Code,

<?php

namespace App\Http\Middleware;

use Closure;
use Illuminate\Http\Request;
use Symfony\Component\HttpFoundation\Response;

class DebugModeOnly
{
    public function handle(Request $request, Closure $next): Response
    {
        if (config('app.debug')) {
            return $next($request);
        }

        return response()->json(['message' => 'Command execution is only allowed in debug mode']);
    }
}
Enter fullscreen mode Exit fullscreen mode

5- Register the Middleware inside the Kernel in $routeMiddleware,

        'DebugModeOnly' => \App\Http\Middleware\DebugModeOnly::class,
Enter fullscreen mode Exit fullscreen mode

Finally, Add Route in Api.php and Assign the Middleware for the Endpoint

Route::get('run-command', [CommandController::class, 'runCommand'])->middleware('DebugModeOnly');
Enter fullscreen mode Exit fullscreen mode

Now, Go and run the Endpoint through Postman

For example, if you want to run php artisan migrate,
You will pass just migrateinto the command attribute inside our API Endpoint.

That's All, Happy Coding ^_^

Summary:
Laravel's Artisan is a powerful command-line interface (CLI) that comes bundled with the Laravel framework, We have created a secure API endpoint in debug mode to facilitate command execution further.

Top comments (0)