Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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?

like image 990
Matius Nugroho Aryanto Avatar asked May 04 '26 18:05

Matius Nugroho Aryanto


2 Answers

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.

like image 66
Praveesh Avatar answered May 07 '26 09:05

Praveesh


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

like image 21
Skel Avatar answered May 07 '26 10:05

Skel



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!