A
A
at0m1x2016-07-09 10:21:12
Yii
at0m1x, 2016-07-09 10:21:12

Yii2 UrlManager - why are optional parameters not substituted?

I am using the Yii2 framework.
The configuration has a rule for UrlManager:

....
'components' => [
    ...
    'urlManager' => [
            'enablePrettyUrl' => true,
            'showScriptName' => false,
            'rules' => [
                ...
                [
                    'pattern' => '<some-param>/some-page',
                    'route' => 'some/route',
                    'defaults' => ['some-param' => 'default-value']
               ],
                ...
            ]
        ],
    ...
]
....

In the code, this route is used in many places:
echo Html::a('Some page', ['some/route']);
But instead of forming a link with the default parameter:
default-value/some-page
, an incorrect link is formed:
some/route
Question: Why can defaults be written in rules if they are not substituted when forming links?
In theory, defaults values ​​​​are needed for this, that if parameters are not specified somewhere when forming a link, then default values ​​\u200b\u200bare needed.

Answer the question

In order to leave comments, you need to log in

1 answer(s)
M
Maxim Timofeev, 2016-07-09
@webinar

1. Perhaps there is a rule that works instead, you need to change their order in this case. Show the full list of rules or move it yourself.
2. This is exactly how it should work

'rules' => [
                   '<someparam>/some-page' => 'some/route',
               ],

and the default value can be set in the action
public function actionRoute($someparam = 'default_value'){
}

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question