2

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!

1

1 Answer 1

2

Fixed this - was due to a couple of issues with this line:

if (Craft::$app->getRequest()->getIsConsoleRequest()) {
  $this->controllerNamespace = 'custom-module\\console\\controllers';
}
  1. My controller was in custom-module/controllers, not custom-module/console/controllers so it was not finding the controller.

  2. 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!

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.