D
D
Daniyar2016-06-23 21:26:40
Yii
Daniyar, 2016-06-23 21:26:40

Yii2 link generation error (Yii2 and multilingual organization) how to fix?

Good afternoon, after reading and applying to my project the article described on Habré (Yii2 and multilingual organization) , I had the following problem when configuring
/frontend/config/main.php

'urlManager' => [
            'enablePrettyUrl' => true,
            'showScriptName' => false,
            'class'=>'frontend\components\LangUrlManager',
            'rules'=>[
                '/' => 'site/index',
                '<controller:\w+>/<action:\w+>/*'=>'<controller>/<action>',
                
                'shop/view/<id:[\d]+>' =>'site/view',
                'shop/category/<category_url:[\w_-]+>/<id:[\d]+>'=>'site/view',
                'shop/category/<category_url:[\w_-]+>'=>'site/category',
            ]
        ],

when generating a link like
<?php echo Html::a("Подробнее", ['shop/view', 'id' => $item->id], ['class' => "btn btn-default"]); ?>

I have generated a link of this kind mysite.ru/ru/shop/view/*?id=1
I tried to get into the code
and added the following in order to remove the asterisk and ?id=
frontend/components/LangUrlManager.php
$url = str_replace("*", "", $url);
if($params['id']){
    $url = str_replace("?id=", '', $url);
}

How to solve the problem so that links are generated normally? Maybe I'm missing something? It is not possible to write a comment on Habré.
Thanks to!

Answer the question

In order to leave comments, you need to log in

1 answer(s)
A
Andrey Inishev, 2016-06-23
@inish777

Most likely, the problem is in the priority of the rules in the config.
is located before
and therefore it is used when forming a link.
I.e,

'urlManager' => [
            'enablePrettyUrl' => true,
            'showScriptName' => false,
            'class'=>'frontend\components\LangUrlManager',
            'rules'=>[
                '/' => 'site/index',
                'shop/view/<id:[\d]+>' =>'site/view',
                'shop/category/<category_url:[\w_-]+>/<id:[\d]+>'=>'site/view',
                'shop/category/<category_url:[\w_-]+>'=>'site/category',
                 '<controller:\w+>/<action:\w+>'=>'<controller>/<action>',
            ]
        ],

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question