Answer the question
In order to leave comments, you need to log in
Yii2 How to implement correctly, adding to all links the parameter - city?
Tell me how easier it is to implement the parameter substitution - city to all urls.
Let's say I wrote the following rules
[
'pattern' => '<city:\w+>/<controller>/<action>/<id:\d+>',
'route' => '<controller>/<action>',
'suffix' => ''
],
[
'pattern' => '<city:\w+>/<controller>/<action>/',
'route' => '<controller>/<action>',
'suffix' => ''
],
[
'pattern' => '<controller>/<action>/<id:\d+>',
'route' => '<controller>/<action>',
'suffix' => ''
],
$city = 'Moscow' // допустим город задан Москва.
$url = Url::toRoute(['/my-controller/my-action', 'id' => 45, 'city' => $city);
Answer the question
In order to leave comments, you need to log in
I solved it by extending the urlManager class and overriding the createUrl method
class MyUrlManager extends UrlManager
{
public function createUrl($params)
{
if(Yii::$app->params['city'])
$params['city'] = Yii::$app->params['city'];
return parent::createUrl($params);
}
}
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question