Search code examples
laravel-5laravel-artisan

Call Artisan Command via URL


In my route I have

Route::get('artisan/{command}/{param}', 'CacheController@show');

And in my controller i have

public function show($id, $param)
{
    $artisan = Artisan::call($id,['flag'=>$param]);
    $output = Artisan::output();
    return $output;
}

I want to be able to call route:cache and cache:clear by accessing domain.com/artisan/cache/clear or route/cache but when I call them it returned something like this

Command "cache" is not defined.

It only called cache, not cache:clear what possibility going wrong?


Solution

  • You are calling an id when you have called it command

    so your function needs to look like this

    public function show($command, $param) {
        $artisan = Artisan::call($command,['flag'=>$param]);
        $output = Artisan::output();
        return $output;
    }
    

    So your URL can look like this domain.com/artisan/cache/clear which means that you are calling this route

    Route::get('artisan/{command}/{param}', 'CacheController@show');
    

    So you need $command intead of $id