Answers.Body: <p>You had the right idea with the RegisterUrlRulesEvent, you just need to specify in the array the url as the key and the path to your controller as the value.</p>
  <pre><code>Event::on(UrlManager::class, UrlManager::EVENT_REGISTER_CP_URL_RULES, function(RegisterUrlRulesEvent $event) {
              $event-&gt;rules += [
                  'my-amazing-plugin-yeah-url' =&gt; 'my-amazing-plugin-yeah/my-controller/my-action',
              ];
          });
  </code></pre>
  
Answers.ContentLicense: CC BY-SA 4.0
Answers.CreationDate: 2023-10-19T19:07:29.337Z
Answers.LastActivityDate: 2023-10-19T19:07:29.337Z
Answers.UserDisplayName: Robin Gauthier
Body: <p>I'm writing a plugin. I'd like it so that a request to view the page is processed via a Controller rather than goes straight to a template (following an MVC pattern).</p>
  <p>So a GET request to the URL of the plugin calls a Controller method, which makes an API call and then displays a template.</p>
  <p>Is this possible in CraftCMS? Can plugins have their own routes file? (I'm used to Laravel)</p>
  <p>This is what I've tried so far:</p>
  <pre><code>public function init(): void
  {
      parent::init();
  
      // Register an event for handling custom routes
      Event::on(
          UrlManager::class,
          UrlManager::EVENT_REGISTER_SITE_URL_RULES,
          function(RegisterUrlRulesEvent $event) {
              $event-&gt;rules[] = [
                  'pattern' =&gt; 'my-amazing-plugin-yeah',
                  'route' =&gt; 'my-amazing-plugin-yeah/my-controller/my-action',
                  'verb' =&gt; ['GET'], // Specify GET as the allowed HTTP method
              ];
          }
      );
  }
  </code></pre>
  <p>I would expect this to break once <code>admin/my-amazing-plugin-yeah</code> was called in the browser, but it's displaying index.php correctly.</p>
  
Tags: <plugin-development><routes><controller>
Title: Can plugins have their own routes?
ThreadText: # Can plugins have their own routes?
  
  (Asked by: username_0 on 2023-10-18)
  
  I'm writing a plugin. I'd like it so that a request to view the page is processed via a Controller rather than goes straight to a template (following an MVC pattern).
  
  So a GET request to the URL of the plugin calls a Controller method, which makes an API call and then displays a template.
  
  Is this possible in CraftCMS? Can plugins have their own routes file? (I'm used to Laravel)
  
  This is what I've tried so far:
  
  ```
  public function init(): void
  {
      parent::init();
  
      // Register an event for handling custom routes
      Event::on(
          UrlManager::class,
          UrlManager::EVENT_REGISTER_SITE_URL_RULES,
          function(RegisterUrlRulesEvent $event) {
              $event->rules[] = [
                  'pattern' => 'my-amazing-plugin-yeah',
                  'route' => 'my-amazing-plugin-yeah/my-controller/my-action',
                  'verb' => ['GET'], // Specify GET as the allowed HTTP method
              ];
          }
      );
  }
  
  ```
  
  I would expect this to break once `admin/my-amazing-plugin-yeah` was called in the browser, but it's displaying index.php correctly.
  
  **Tags:** plugin-development, routes, controller
  
  
  ---
  
  ## 1 answers
  
  ### Answer by username_1 on 2023-10-19. Score: 1
  
  You had the right idea with the RegisterUrlRulesEvent, you just need to specify in the array the url as the key and the path to your controller as the value.
  
  ```
  Event::on(UrlManager::class, UrlManager::EVENT_REGISTER_CP_URL_RULES, function(RegisterUrlRulesEvent $event) {
              $event->rules += [
                  'my-amazing-plugin-yeah-url' => 'my-amazing-plugin-yeah/my-controller/my-action',
              ];
          });
  
  ```
  
  
  ---
  
  