DEV Community

Cover image for Running Artisan Commands from Your Browser Using Laravel's Artisan Facade
Snehal Rajeev Moon
Snehal Rajeev Moon

Posted on • Edited on

Running Artisan Commands from Your Browser Using Laravel's Artisan Facade

Introduction:

Laravel's Artisan command-line tool is a powerful tool for managing various aspects of your Laravel application. While it's primarily designed for the command line, there are situations where you might want to trigger Artisan commands from a web browser. In this blog post, we'll explore how you can achieve this using Laravel's Artisan Facade.

Prerequisites:

Before we dive into running Artisan commands from a browser, ensure you have the following prerequisites in place:

  1. A Laravel application was installed and set up.
  2. Basic knowledge of Laravel's Artisan commands.
  3. A web server environment, such as Apache or Nginx, to host your Laravel application.

Step 1: Import the Artisan Facade:

To start, create a controller where we will perform the logic when you trigger the Artisan command from the browser and import the Artisan Facade at the top of the file:

  • Create a controller by executing the following command.
php artisan make:controller ExecuteArtisanCommandController --invokable
Enter fullscreen mode Exit fullscreen mode
  • Open the controller import the Artisan facade and write the given code in invoke method code.
namespace App\Http\Controllers;

use Illuminate\Http\Request;
use Illuminate\Support\Facades\Artisan; 

public function __invoke(Request $request, $command)
{
   $params = $request->all();
   Artisan::call($command, $params);
   $output = Artisan::output();
}
Enter fullscreen mode Exit fullscreen mode

Step 2: Create a Route:

Now, create a route in your Laravel application's routes/web.php file to trigger the Artisan command when a specific URL is accessed.

Route::get('/run-command/{name_of_command}', ExecuteArtisanCommandController::class);
Enter fullscreen mode Exit fullscreen mode

Step 3: Access the Command via the Browser:

Now, when you access the /run-command/{name_of_command} URL in your web browser, Laravel will execute the specified Artisan command.

http://your_web_url/run-command/migrate
Enter fullscreen mode Exit fullscreen mode

In the above case, we are executing the migrate command from the browser /run-command/migrate will trigger the given command. In the same way, you can also run your custom commands in the browser and check the output.

Conclusion:

In this blog post, we've explored how to run Laravel Artisan commands from a web browser using the Artisan Facade. This can be a handy feature for specific use cases, such as triggering maintenance tasks or database migrations or executing your custom command directly from a user-friendly interface. However, exercise caution and ensure proper access controls when implementing this functionality to maintain the security and integrity of your Laravel application.

Happy Reading 🦄 ❤️

Top comments (0)