1

I am in the process of building a plugin that has a controller that accepts a HTTP request. If the token and request are valid, I would like to call a Craft Console Command from within the plugin (e.g. gc/run or resave/entries).

The equivalent would be using the CLI for call the command ./craft gc/run but from a controller.

How would I call the command from my plugins HTTP controller?

  • Do you need to call some build in command or your own? Usually I wrap functions in Service Classes so I can call them both from controllers and/or console commands. – naboovalley Oct 12 '19 at 6:17
  • I’m calling a built in command. Running in a Docker environment so I cannot SSH into the machine. – Jason McCallister Oct 12 '19 at 11:57
1

Since Yii treats console commands like requests, you need to perform the following steps to execute a Craft console command from within a plugin:

  1. Within your plugin, set the namespace to match the commands namespace
  2. Call a run action from your plugin, and pass the route name/params
    MyPlugin::$plugin->getInstance()->controllerNamespace = 'craft\console\controllers';

    $command = MyPlugin::$plugin->getInstance()->runAction('gc/run');

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.