Answers.Body: <p>The <code>EVENT_REGISTER_CP_URL_RULES</code> 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 <code>EVENT_REGISTER_SITE_URL_RULES</code> instead.</p>
  <p>You can see the logic around this <a href="https://github.com/craftcms/cms/blob/86dcff6444f0bda0ca56af2fd83d4e4cbcddeeb6/src/web/UrlManager.php#L311" rel="nofollow noreferrer">here</a>.</p>
  
Answers.ContentLicense: CC BY-SA 4.0
Answers.CreationDate: 2021-07-22T17:06:25.920Z
Answers.Text: 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.
Answers.UserDisplayName: braican
Answers.ContentLicense: CC BY-SA 4.0
Answers.CreationDate: 2021-07-22T17:07:22.567Z
Answers.Text: 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.
Answers.UserDisplayName: braican
Answers.ContentLicense: CC BY-SA 4.0
Answers.CreationDate: 2021-07-22T06:11:38.810Z
Answers.LastActivityDate: 2021-07-22T06:11:38.810Z
Answers.UserDisplayName: Evan Warner
Body: <p>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 <a href="https://craftcms.com/docs/3.x/extend/cp-edit-pages.html" rel="nofollow noreferrer">creating edit pages</a>, I hit the point where I'm trying to register my control panel routes via the <code>EVENT_REGISTER_CP_URL_RULES</code> 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:</p>
  <pre class="lang-php prettyprint-override"><code>&lt;?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-&gt;types[] = EventElement::class;
              }
          );
  
          // This is not firing.
          Event::on(
              UrlManager::class,
              UrlManager::EVENT_REGISTER_CP_URL_RULES,
              function (RegisterUrlRulesEvent $event) {
  
                  Craft::info(&quot;Add my routes.&quot;); // &lt;- This _doesn't_ appear in the logs
  
                  $event-&gt;rules['event-element/events/new'] = 'event-element/events/edit';
                  $event-&gt;rules['event-element/events/&lt;eventId:\d+&gt;'] = 'event-element/events/edit';
              }
          );
  
  
          Craft::info('Event Element plugin loaded'); // &lt;- I see this in the logs.
      }
  
  
  }
  
  </code></pre>
  <p>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 <code>init()</code> method are firing as well; the <code>EVENT_REGISTER_ELEMENT_TYPES</code> is working to add my custom Event element type.</p>
  <p>What's strange is that I'm using the <a href="https://github.com/verbb/events" rel="nofollow noreferrer">verbb/events</a> plugin as a reference guide for some of what I'm doing and they have a listener set up for the same <code>EVENT_REGISTER_CP_URL_RULES</code> event that's working just fine (<a href="https://github.com/verbb/events/blob/df88bb70e462f21198ad50736a791cef9178d001/src/Events.php#L152" rel="nofollow noreferrer">GitHub source here</a>). I know this is running because I added a log to their event callback that I am seeing reflected in my logs.</p>
  <p>Additionally, I tried adding a callback for the <code>EVENT_REGISTER_SITE_URL_RULES</code> event for front-end routes, and that listener doesn't appear to be firing either.</p>
  <p>Am I missing anything else I need to do to set up these event listeners?</p>
  
Comments.ContentLicense: CC BY-SA 4.0
Comments.CreationDate: 2021-07-22T06:11:28.697Z
Comments.Text: Oh hey Nick! :D
Comments.UserDisplayName: Evan Warner
Tags: <plugin-development><craft3><events>
Title: UrlManager::EVENT_REGISTER_CP_URL_RULES event not firing in custom plugin
ThreadText: # UrlManager::EVENT_REGISTER_CP_URL_RULES event not firing in custom plugin
  
  (Asked by: username_0 on 2021-07-22)
  
  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](https://craftcms.com/docs/3.x/extend/cp-edit-pages.html), 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
  <?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](https://github.com/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](https://github.com/verbb/events/blob/df88bb70e462f21198ad50736a791cef9178d001/src/Events.php#L152)). 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?
  
  **Tags:** plugin-development, craft3, events
  
  > **Comments:**
  > **username_1**: Oh hey Nick! :D  
  
  ---
  
  ## 1 answers
  
  ### Answer by username_1 on 2021-07-22. Score: 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](https://github.com/craftcms/cms/blob/86dcff6444f0bda0ca56af2fd83d4e4cbcddeeb6/src/web/UrlManager.php#L311).
  
  > **Comments:**
  > **username_0**: Yo username_1! 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.  
  > **username_0**: 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.  
  
  ---
  
  