1

I'm working on building out a custom plugin for Craft 3 to create a new element type called Events. Following the Craft docs for creating edit pages, I hit the point where I'm trying to register my control panel routes via the EVENT_REGISTER_CP_URL_RULES event. For some reason, however, the event listener in my plugin doesn't appear to be getting called for that event. Here's my main plugin class:

<?php
namespace mynamespace\eventelement;

use Craft;
use craft\base\Plugin;
use craft\events\RegisterUrlRulesEvent;
use craft\web\Urlmanager;

use yii\base\Event;

class EventElementPlugin extends Plugin
{
    public static $plugin;

    public $schemaVersion = '1.0.0';
    public $hasCpSettings = false;
    public $hasCpSection = true;

    public function init()
    {
        parent::init();
        self::$plugin = $this;

        // Register the EventElement. This is firing.
        Event::on(
            Elements::class,
            Elements::EVENT_REGISTER_ELEMENT_TYPES,
            function (RegisterComponentTypesEvent $event) {
                $event->types[] = EventElement::class;
            }
        );

        // This is not firing.
        Event::on(
            UrlManager::class,
            UrlManager::EVENT_REGISTER_CP_URL_RULES,
            function (RegisterUrlRulesEvent $event) {

                Craft::info("Add my routes."); // <- This _doesn't_ appear in the logs

                $event->rules['event-element/events/new'] = 'event-element/events/edit';
                $event->rules['event-element/events/<eventId:\d+>'] = 'event-element/events/edit';
            }
        );


        Craft::info('Event Element plugin loaded'); // <- I see this in the logs.
    }


}

When loading the site, my logging message inside the event listener doesn't fire and those routes 404 since they don't get added. I know the plugin is being loaded though, since I'm seeing the 'Event Element plugin loaded' text in my logs. I'll add that the other event listeners I have in my init() method are firing as well; the EVENT_REGISTER_ELEMENT_TYPES is working to add my custom Event element type.

What's strange is that I'm using the verbb/events plugin as a reference guide for some of what I'm doing and they have a listener set up for the same EVENT_REGISTER_CP_URL_RULES event that's working just fine (GitHub source here). I know this is running because I added a log to their event callback that I am seeing reflected in my logs.

Additionally, I tried adding a callback for the EVENT_REGISTER_SITE_URL_RULES event for front-end routes, and that listener doesn't appear to be firing either.

Am I missing anything else I need to do to set up these event listeners?

1
  • 1
    Oh hey Nick! :D Jul 22, 2021 at 6:11

1 Answer 1

1

The EVENT_REGISTER_CP_URL_RULES event only fires if you're loading a page in the Craft admin itself. If you're loading your site frontend it won't fire, you need to use EVENT_REGISTER_SITE_URL_RULES instead.

You can see the logic around this here.

2
  • Yo Evan! Sorry if I wasn't clear - I am in the admin. I've got an index page in the CP to list all of my Event elements; when I reload this admin screen I'm seeing the logging message I've added to the EVENT_REGISTER_CP_URL_RULES in the verbb/events plugin, but not the logging message in my plugin for the same event callback.
    – braican
    Jul 22, 2021 at 17:06
  • Incidentally I also just tried adding a callback for the EVENT_REGISTER_SITE_URL_RULES event in my plugin, and that also doesn't seem to be firing when I load a page on the frontend.
    – braican
    Jul 22, 2021 at 17:07

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.