Skip links

Using artisan commands in routes / Controllers / Outside CLI in Laravel

There are numerous times when we need to use artisan commands outside CLI. with below command you can use artisan commands outside CLI.

Route::get('/foo', function()
{
    $exitCode = Artisan::call('command:name', ['--option' => 'foo']);

    //
});

Clearing Laravel cache without CLI

Route::get('/clearCache', function()
{
Artisan::call('cache:clear');
return Artisan::output(); //Get the output for the last run command.
});

Migrate without CLI

Route::get('/migrate', function()
{
Artisan::call('migrate');
return Artisan::output(); //Get the output for the last run command.
});

Create Laravel Controller without CLI

Route::get('/createController', function()
{
  $exitCode =  Artisan::call('make:controller', ['name' => 'TestingController']);
return Artisan::output(); //Get the output for the last run command.
});

Create Model without CLI

Route::get('/createModel', function()
{
  $exitCode =  Artisan::call('make:model', ['name' => 'Testing']);
return Artisan::output(); //Get the output for the last run command.
});

Create Storage Link without CLI

Route::get('/link', function () {
    Artisan::call('storage:link');
return Artisan::output();
});

You can try and create different routes using different commands. The more you practice, the better you become. Best of Luck!

Leave a comment