D
D
Dmitry2020-10-20 11:11:29
Yii
Dmitry, 2020-10-20 11:11:29

How to make a rule for url?

Good afternoon, there is an address of this kind namesite.com/catalog/1?brand=1
I want to make that there was such an address namesite.com/catalog/1/brand-1 (you need to remove "?=")

I wrote in the urlManager rules:

'urlManager' => [
            'enablePrettyUrl' => true,
            'showScriptName' => false,
            'rules' => [
                'catalog/<id:.+>/brand/<brand:[\w-]+>' => 'cat/view',
                'catalog/<id:.+>' => 'cat/view',
                'catalog' => 'cat/view',
                'brand/<id:.+>' => 'brnd/view',
                'product/<id:.+>' => 'prod/view',
            ],
        ],


I create a link like this:
<?= Html::a($b->name, ['catalog/'.$model->id.'', 'brand' => $b->id]) ?>


Can you please tell me how to form such kind of url namesite.com/catalog/1/brand-1 ??

Answer the question

In order to leave comments, you need to log in

1 answer(s)
D
Dmitry, 2020-10-20
@JoneCode

'default/<id:\d+>/brand-<brand:\d+>' => 'default/index',
'default/<id:\d+>' => 'default/index',
'default' => 'default/index',

echo Html::a('test', ['default/index', 'id' => $id, 'brand' =>  $brand])
echo Html::a('test', ['default/index', 'id' => $id])
echo Html::a('test', ['default/index'])

http://site.loc/default/index/3/brand-2
http://site.loc/default/index/3
http://site.loc/default/

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question