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. Oct 12, 2019 at 6:17
  • I’m calling a built in command. Running in a Docker environment so I cannot SSH into the machine. Oct 12, 2019 at 11:57

1 Answer 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 and acknowledge you have read our privacy policy.

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