<p>Fixed this - was due to a couple of issues with this line:</p>
<pre><code>if (Craft::$app-&gt;getRequest()-&gt;getIsConsoleRequest()) {
  $this-&gt;controllerNamespace = 'custom-module\\console\\controllers';
}
</code></pre>
<ol>
<li><p>My controller was in <code>custom-module/controllers</code>, not <code>custom-module/console/controllers</code> so it was not finding the controller.</p>
</li>
<li><p>My module is namespaced as <code>namespace modules\custom-module</code> so I needed to add <code>modules\\</code> to my controller namespace override: <code>$this-&gt;controllerNamespace = 'modules\\custom-module\\console\\controllers'</code></p>
</li>
</ol>
<p>Now <code>./craft custom-module/custom-controller/custom-action</code> works!</p>

CC BY-SA 4.0
2022-05-06T17:53:21.547Z
2022-05-06T17:53:21.547Z
iamkeir
<p>I have built a custom module with some custom controller actions - some of which I would like to run via console, but I am not sure what the path/command should be for running these actions?</p>
<p>In my <code>routes.php</code> I have:
<code>'action/my-action' =&gt; 'custom-module/custom-controller/custom-action'</code></p>
<p>Is it based on the URL route, or the module command, or something else?</p>
<p>Things like <code>./craft action/my-action</code> or <code>./craft custom-module/custom-controller/custom-action</code> do not work and return:</p>
<pre><code>Unknown command: custom-module/custom-controller/custom-action
Caused by: Exception 'yii\base\InvalidRouteException' with message 'Unable to resolve the request &quot;custom-module/custom-controller/custom-action&quot;.'
</code></pre>
<p>We have got the following in our module which I thought would make web controllers &quot;just work&quot; but it does not:</p>
<pre><code>if (Craft::$app-&gt;getRequest()-&gt;getIsConsoleRequest()) {
  $this-&gt;controllerNamespace = 'custom-module\\console\\controllers';
}
</code></pre>
<p>And an example controller/action: <a href="https://gist.github.com/iamkeir/5929a7da368e9b74e93c330f504d0bd1" rel="nofollow noreferrer">https://gist.github.com/iamkeir/5929a7da368e9b74e93c330f504d0bd1</a></p>
<p>Thanks!</p>

CC BY-SA 4.0
2022-05-06T16:05:40.953Z
Related: https://craftcms.stackexchange.com/questions/27321/how-can-i-call-console-controllers
iamkeir
<modules><custom><cli><console>
How to run custom module controller actions from console?
# How to run custom module controller actions from console?

(Asked by: username_0 on 2022-05-06)

I have built a custom module with some custom controller actions - some of which I would like to run via console, but I am not sure what the path/command should be for running these actions?
In my routes.php I have:
'action/my-action' => 'custom-module/custom-controller/custom-action'
Is it based on the URL route, or the module command, or something else?
Things like ./craft action/my-action or ./craft custom-module/custom-controller/custom-action do not work and return:
Unknown command: custom-module/custom-controller/custom-action
Caused by: Exception 'yii\base\InvalidRouteException' with message 'Unable to resolve the request "custom-module/custom-controller/custom-action".'

We have got the following in our module which I thought would make web controllers "just work" but it does not:
if (Craft::$app->getRequest()->getIsConsoleRequest()) {
  $this->controllerNamespace = 'custom-module\\console\\controllers';
}

And an example controller/action: https://gist.github.com/iamkeir/5929a7da368e9b74e93c330f504d0bd1
Thanks!

**Tags:** modules, custom, cli, console
> **Comments:**
> **username_0**: Related: https://craftcms.stackexchange.com/questions/27321/how-can-i-call-console-controllers  

---

## 1 answers

### Answer by username_0 on 2022-05-06. Score: 2 | Accepted answer

Fixed this - was due to a couple of issues with this line:
if (Craft::$app->getRequest()->getIsConsoleRequest()) {
  $this->controllerNamespace = 'custom-module\\console\\controllers';
}


My controller was in custom-module/controllers, not custom-module/console/controllers so it was not finding the controller.

My module is namespaced as namespace modules\custom-module so I needed to add modules\\ to my controller namespace override: $this->controllerNamespace = 'modules\\custom-module\\console\\controllers'


Now ./craft custom-module/custom-controller/custom-action works!


---

