1

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!

5
  • What's the “ID” of your module in the modules block of your app.php file? Oct 9, 2021 at 1:11
  • @AugustMiller: hi, thank you. the modules app is set like this: 'modules' => [ 'password-global' => astro\passwordglobal\PasswordGlobal::class, ], 'bootstrap' => ['password-global'],
    – mauricio
    Oct 9, 2021 at 1:13
  • 1
    Aha! Ok—try making a request to ?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. Oct 9, 2021 at 1:16
  • All right, I tried it out, but I get the same error: Unable to resolve the request "https:/mysite.nitro/index.php?p=actions/password-global/default".
    – mauricio
    Oct 9, 2021 at 1:25
  • @AugustMiller, You were right. Thank you so much. After many attempts I found that I had a compounding issue with my ajax call, that make it hard to figure what was going on. After sorting things out with my JS script your indication worked out great.
    – mauricio
    Oct 9, 2021 at 15:10

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.