Calling Craft Console Commands from Plugin? 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?
Since Yii treats console commands like requests, you need to perform the following steps to execute a Craft console command from within a plugin:


Within your plugin, set the namespace to match the commands namespace
Call a run action from your plugin, and pass the route name/params


    MyPlugin::$plugin-&gt;getInstance()-&gt;controllerNamespace = 'craft\console\controllers';

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