<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>

CC BY-SA 4.0
2019-10-01T13:29:53.127Z
2019-10-01T13:29:53.127Z
foamcow
<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>

CC BY-SA 4.0
2019-10-01T12:08:39.327Z
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.
Eternal Black
CC BY-SA 4.0
2019-10-01T12:14:14.157Z
I could do that, yes. Was more curious as to if it were possible. I would prefer to keep the "nice" url without parameters.
foamcow
<plugin-development><routes><modules>
Accessing named parameter in custom route from within a controller
# 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//pause'] = 'custom-module-name/order/pause-order';
    }
);

This works fine but how can I access that  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//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!


---

