I'm new to craft and slowly learning how to use more advanced features. I'm trying to write a module with a controller. When the route is invoked on a fetch call I get the following error:
HTTP 404 – Not Found – yii\web\NotFoundHttpException
Page not found.
Caused by: Invalid Route – yii\base\InvalidRouteException
Unable to resolve the request "https:/mysite.nitro/index.php?p=actions/astro/password-global/default".
I would appreciate any pointers on why I'm getting this error. I have reviewed similar questions on SE for hours, but nothing seems to work.
I'm using a template generated by pluginfactory.io, which I simplified and adapted a little.
The main class, PasswordGlobal.php, code is this:
namespace astro\passwordglobal;
use astro\passwordglobal\services\PasswordGlobalService as PasswordGlobalService;
use astro\passwordglobal\twigextensions\PasswordGlobalExtension;
class PasswordGlobal extends Module
{
public static $instance;
public function __construct($id, $parent = null, array $config = [])
{
Craft::setAlias('@astro/passwordglobal', $this->getBasePath());
$this->controllerNamespace = 'astro\\passwordglobal\\controllers';
static::setInstance($this);
parent::__construct($id, $parent, $config);
}
public function init()
{
// Executes only on the site's front-end.
// if (! Craft::$app->request->getIsSiteRequest()) { return false; }
parent::init();
self::$instance = $this;
// Registers services
$this->setComponents([
'passwordGlobalService' => PasswordGlobalService::class,
]);
// Register our site routes
Event::on(
UrlManager::class,
UrlManager::EVENT_REGISTER_SITE_URL_RULES,
function (RegisterUrlRulesEvent $event) {
$event->rules['passwordGlobalForm'] = 'astro/password-global/default';
}
);
// Add in our Twig extensions
Craft::$app->view->registerTwigExtension(new PasswordGlobalExtension());
}
And the controller: DefaultController.php
namespace astro\passwordglobal\controllers;
use astro\passwordglobal\PasswordGlobal;
class DefaultController extends Controller
{
protected $allowAnonymous = ['index', 'do-something'];
public function actionIndex()
{
$result = 'Welcome to the DefaultController actionIndex() method';
return $result;
}
public function actionDoSomething()
{
$result = 'Welcome to the DefaultController actionDoSomething() method';
return $result;
}
}
I'm quite certain the module is loading. I created a twig extension which is loaded by the main class. I can call it on the front end and see the expected output. So the issue is how I'm setting up the route to the controller.
And just in case, my directory structure inside modules is:
|-astro
|---passwordglobal
|-----src
|-------controllers
|---------DefaultController.php
|-------models
|---------PasswordGlobalModel.php
|-------services
|---------PasswordGlobalService.php
|-------twigextensions
|---------PasswordGlobalExtension.php
|-------PasswordGlobal.php
Thank you!
modules
block of yourapp.php
file??p=actions/password-global/default
or?p=actions/password-global/default/do-something
. I believe your Module's ID is used as the first segment in the action path, not the namespace! Here's the relevant Yii documentation on routing to modules.Unable to resolve the request "https:/mysite.nitro/index.php?p=actions/password-global/default".