<p>I had a hunch that a web controller with a similar name was getting called, and indeed that is what was happening. In my module's init method I was setting the controller namespace like this:</p>

<pre class="lang-php prettyprint-override"><code>$this-&gt;controllerNamespace = 'modules\vetscripts\controllers';`
</code></pre>

<p>By default, then, the console was going to the web controller, I'm assuming. @Selvin pointed me in the right direction with this:</p>

<pre class="lang-php prettyprint-override"><code>use craft\console\Application as Console

...

public function init()
{
    ...
    if (Craft::$app instanceof Console) {
        $this-&gt;controllerNamespace = 'modules\vetscripts\console\controllers';
    }
    ...
}
</code></pre>

<p>Now I can see my module's custom console actions when running <code>./craft</code> from the console.</p>

CC BY-SA 4.0
2022-05-06T15:58:23.870Z
How do you actually run the command in console? I've tried `./craft my-module/controller-name/action-name` but this doesn't seem to work
iamkeir
CC BY-SA 4.0
2019-08-05T18:46:23.220Z
2019-08-05T18:46:23.220Z
Steven Thate
<p>I have a module with a console controller (<code>ScriptsController</code>):</p>

<pre class="lang-php prettyprint-override"><code>namespace modules\vetscripts\console\controllers;

use yii\console\Controller;
use yii\console\ExitCode;
use modules\vetscripts\VetScripts;

class ScriptsController extends Controller
{
    public function actionExpiring()
    {
        VetScripts::$instance-&gt;scripts-&gt;expiringScripts();

        return ExitCode::OK;
    }
}
</code></pre>

<p>When I run it using <code>$ ./craft vetscripts/scripts/expiring</code> I get the following error and stack trace:</p>

<pre><code>2019-08-05 11:35:24 [-][-][-][error][yii\base\UnknownMethodException] yii\base\UnknownMethodException: Calling unknown method: craft\console\Request::getAcceptsJson() in /Users/steven/Code/vetscripts/vendor/yiisoft/yii2/base/Component.php:300
Stack trace:
#0 /Users/steven/Code/vetscripts/vendor/craftcms/cms/src/web/Controller.php(189): yii\base\Component-&gt;__call('getAcceptsJson', Array)
#1 /Users/steven/Code/vetscripts/vendor/yiisoft/yii2/base/Module.php(528): craft\web\Controller-&gt;runAction('expiring', Array)
#2 /Users/steven/Code/vetscripts/vendor/yiisoft/yii2/console/Application.php(180): yii\base\Module-&gt;runAction('vetscripts/scri...', Array)
#3 /Users/steven/Code/vetscripts/vendor/craftcms/cms/src/console/Application.php(93): yii\console\Application-&gt;runAction('vetscripts/scri...', Array)
#4 /Users/steven/Code/vetscripts/vendor/yiisoft/yii2/console/Application.php(147): craft\console\Application-&gt;runAction('vetscripts/scri...', Array)
#5 /Users/steven/Code/vetscripts/vendor/yiisoft/yii2/base/Application.php(386): yii\console\Application-&gt;handleRequest(Object(craft\console\Request))
#6 /Users/steven/Code/vetscripts/craft(22): yii\base\Application-&gt;run()
#7 {main}
</code></pre>

<p>This is my first time attempting a custom console command, which I need to get a cron job up and running. Is there something I'm missing here?</p>

<p>Thanks!</p>

<console>
Why is my custom console command throwing Calling unknown method: ...getAcceptsJson?
# Why is my custom console command throwing Calling unknown method: ...getAcceptsJson?

(Asked by: username_0 on 2019-08-05)

I have a module with a console controller (ScriptsController):
namespace modules\vetscripts\console\controllers;

use yii\console\Controller;
use yii\console\ExitCode;
use modules\vetscripts\VetScripts;

class ScriptsController extends Controller
{
    public function actionExpiring()
    {
        VetScripts::$instance->scripts->expiringScripts();

        return ExitCode::OK;
    }
}

When I run it using $ ./craft vetscripts/scripts/expiring I get the following error and stack trace:
2019-08-05 11:35:24 [-][-][-][error][yii\base\UnknownMethodException] yii\base\UnknownMethodException: Calling unknown method: craft\console\Request::getAcceptsJson() in /Users/steven/Code/vetscripts/vendor/yiisoft/yii2/base/Component.php:300
Stack trace:
#0 /Users/steven/Code/vetscripts/vendor/craftcms/cms/src/web/Controller.php(189): yii\base\Component->__call('getAcceptsJson', Array)
#1 /Users/steven/Code/vetscripts/vendor/yiisoft/yii2/base/Module.php(528): craft\web\Controller->runAction('expiring', Array)
#2 /Users/steven/Code/vetscripts/vendor/yiisoft/yii2/console/Application.php(180): yii\base\Module->runAction('vetscripts/scri...', Array)
#3 /Users/steven/Code/vetscripts/vendor/craftcms/cms/src/console/Application.php(93): yii\console\Application->runAction('vetscripts/scri...', Array)
#4 /Users/steven/Code/vetscripts/vendor/yiisoft/yii2/console/Application.php(147): craft\console\Application->runAction('vetscripts/scri...', Array)
#5 /Users/steven/Code/vetscripts/vendor/yiisoft/yii2/base/Application.php(386): yii\console\Application->handleRequest(Object(craft\console\Request))
#6 /Users/steven/Code/vetscripts/craft(22): yii\base\Application->run()
#7 {main}

This is my first time attempting a custom console command, which I need to get a cron job up and running. Is there something I'm missing here?
Thanks!

**Tags:** console

---

## 1 answers

### Answer by username_0 on 2019-08-05. Score: 2 | Accepted answer

I had a hunch that a web controller with a similar name was getting called, and indeed that is what was happening. In my module's init method I was setting the controller namespace like this:
$this->controllerNamespace = 'modules\vetscripts\controllers';`

By default, then, the console was going to the web controller, I'm assuming. @Selvin pointed me in the right direction with this:
use craft\console\Application as Console

...

public function init()
{
    ...
    if (Craft::$app instanceof Console) {
        $this->controllerNamespace = 'modules\vetscripts\console\controllers';
    }
    ...
}

Now I can see my module's custom console actions when running ./craft from the console.

> **Comments:**
> **username_1**: How do you actually run the command in console? I've tried `./craft my-module/controller-name/action-name` but this doesn't seem to work  

---

