Answers.Body: <p>Hat tip to Andrew Welch for the answer.</p>
  
  <p>With the route <code>my-account/orders/&lt;order&gt;/pause</code> it's possible to simply add the parameter to my controller method definition and it gets populated from the named parameter in the route.</p>
  
  <pre><code>  public function actionPauseOrder($order = null)
      {
  
         // do stuff
      }
  </code></pre>
  
  <p>The value of the named parameter <code>order</code> can then be accessed in the function as one would expect.</p>
  
  <p>Super simple!</p>
  
Answers.ContentLicense: CC BY-SA 4.0
Answers.CreationDate: 2019-10-01T13:29:53.127Z
Answers.LastActivityDate: 2019-10-01T13:29:53.127Z
Answers.UserDisplayName: foamcow
Body: <p>I have a custom module in which I am setting a front end route that routes through to a controller. Like so:</p>
  
  <pre><code> Event::on(
      UrlManager::class,
      UrlManager::EVENT_REGISTER_SITE_URL_RULES,
      function (RegisterUrlRulesEvent $event) {
          $event-&gt;rules['my-account/orders/&lt;order&gt;/pause'] = 'custom-module-name/order/pause-order';
      }
  );
  </code></pre>
  
  <p>This works fine but how can I access that <code>&lt;order&gt;</code> named parameter within my controller? Is the only way to use <code>craft::$app-&gt;request-&gt;getSegment(3);</code>?</p>
  
Comments.ContentLicense: CC BY-SA 4.0
Comments.CreationDate: 2019-10-01T12:08:39.327Z
Comments.Text: If this is possible to you, you could instead use an endpoint like `my-account/orders/<order>?action=pause` and access the `pause` via `$param = Craft::$app->getRequest()->getParam('action');` Might be an easier solution.
Comments.UserDisplayName: Eternal Black
Comments.ContentLicense: CC BY-SA 4.0
Comments.CreationDate: 2019-10-01T12:14:14.157Z
Comments.Text: I could do that, yes. Was more curious as to if it were possible. I would prefer to keep the "nice" url without parameters.
Comments.UserDisplayName: foamcow
Tags: <plugin-development><routes><modules>
Title: Accessing named parameter in custom route from within a controller
ThreadText: # Accessing named parameter in custom route from within a controller
  
  (Asked by: username_0 on 2019-10-01)
  
  I have a custom module in which I am setting a front end route that routes through to a controller. Like so:
  
  ```
   Event::on(
      UrlManager::class,
      UrlManager::EVENT_REGISTER_SITE_URL_RULES,
      function (RegisterUrlRulesEvent $event) {
          $event->rules['my-account/orders/<order>/pause'] = 'custom-module-name/order/pause-order';
      }
  );
  
  ```
  
  This works fine but how can I access that `<order>` named parameter within my controller? Is the only way to use `craft::$app->request->getSegment(3);`?
  
  **Tags:** plugin-development, routes, modules
  
  > **Comments:**
  > **username_1**: If this is possible to you, you could instead use an endpoint like `my-account/orders/<order>?action=pause` and access the `pause` via `$param = Craft::$app->getRequest()->getParam('action');` Might be an easier solution.  
  > **username_0**: I could do that, yes. Was more curious as to if it were possible. I would prefer to keep the "nice" url without parameters.  
  
  ---
  
  ## 1 answers
  
  ### Answer by username_0 on 2019-10-01. Score: 1
  
  Hat tip to Andrew Welch for the answer.
  
  With the route `my-account/orders/<order>/pause` it's possible to simply add the parameter to my controller method definition and it gets populated from the named parameter in the route.
  
  ```
    public function actionPauseOrder($order = null)
      {
  
         // do stuff
      }
  
  ```
  
  The value of the named parameter `order` can then be accessed in the function as one would expect.
  
  Super simple!
  
  
  ---
  
  