In Laravel, you can call an Artisan command from a route using the
Artisanfacade. Here is an example of how to do this:
Create an Artisan command using the make:command Artisan command:
php artisan make:command ExampleCommand
In the newly created ExampleCommand class, define the logic for the command.
class ExampleCommand extends Command
{
protected $signature = 'example:run';
protected $description = 'Run the example command';
public function handle()
{
$this->info('Example command run successfully!');
}
}
In your routes/web.php file, define a route that calls the Artisan command:
Route::get('example', function () {
\Artisan::call('example:run');
return 'Example command run successfully!';
});
Now, when you visit http://your-app.test/example in your browser, the Artisan command example:run will be executed and you will see the message “Example command run successfully!” displayed.
Note: Replace your-app.test with the actual URL of your Laravel application.
