Yii's named parameters are available in the template. For example, for the following route configuration:

'post/<post_id:\d+>' => ['template' => 'post-entry'],

The value of post_id in my template for the request post/3 will be 3.

But how can I access it from PHP, for example from the Craft::app -> getRequest() ?

Note: I don't mean queryString parameters. QueryString parameters can be accessed using getParam('paramName').

2 Answers 2


If you want to explicitly grab them from PHP, this should also work:

$result = Craft::$app->getRequest()->resolve();

From there, you could grab $result[1], which should be an array:

    'variables' => array(
        'post_id' => 3,
    'template' => 'post-entry',
    'p' => 'post/3',
  • For the routes.php configuration: 'post/<post_id:\d+>' => [ 'template' => 'post-entry'], the value of $params is array( "template"=> "post-entry)" No clue of post_id but it's available in the template when I do {{ post_id }} Did I misunderstand something? May 11, 2018 at 8:33
  • It's also not available in the template if I do {{ dump(craft.app.urlManager.routeParams()) }}, this displays: array(1) { ["template"]=> string(9) "post-entry" } . But {{ post_id }} => 3 So a) I am missing an extra configuration or b) it's not there... May 11, 2018 at 9:05
  • Updated answer... should be more reliable and take into account your route.
    – Brad Bell
    May 11, 2018 at 23:46

It is passed as a parameter to your function

public function actionView($post_id){

Note: your parameter must have the same name like defined in your route. You can't access it with public function actionView($postId)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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