1

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);?

  • 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 Oct 1 '19 at 12:08
  • 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 Oct 1 '19 at 12:14
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!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.