Matius Nugroho Aryanto
Matius Nugroho Aryanto

Reputation: 901

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?

Upvotes: 3

Views: 3857

Answers (2)

Skel
Skel

Reputation: 1667

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

Upvotes: 3

Praveesh
Praveesh

Reputation: 1327

Modify your code like this:

    $artisan = \Artisan::call($command.":".$param);
    $output = \Artisan::output();
    return $output;

Flag argument is used to pass the arguments of an artisan command.

Upvotes: 4

Related Questions