How to run artisan command in shared hosting from route or controller

Laravel is the most modern and popular framework in PHP. One of the most powerful tools in Laravel is artisan command. Most of the time we host our site in shared server and most of the shared server does not support or provide command line interface. So what to do if we need to run artisan command. Here is how we can do it easily in Laravel from route. We will use the call method on the Artisan facade to run command.

 

Route::get('artisan/command/{key?}', array(function($key = null)
{

if($key == "cache-clear"){
 try {
 echo '<br>php artisan cache:clear...';
 Artisan::call('cache:clear');
 echo '<br>php artisan cache:clear completed';
 } catch (Exception $e) {
 Response::make($e->getMessage(), 500);
 }
 }elseif($key == "view-clear"){
 try {
 echo '<br>php artisan view:clear...';
 Artisan::call('view:clear');
 echo '<br>php artisan view:clear completed';
 
 } catch (Exception $e) {
 Response::make($e->getMessage(), 500);
 }
 
 }else{
 App::abort(404);
 }

}
));

Leave a Comment

Your email address will not be published. Required fields are marked *