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?