M
M
Max DangerPro2016-11-23 16:57:01
Yii
Max DangerPro, 2016-11-23 16:57:01

How to compose a regular expression in Yii2?

There is an address:
sitename.ru/category/abc?letter=С
What regular expression is needed to be like this:
sitename.ru/category/abc/С
The last parameter changes, these are letters of the alphabet.
And also, there is another address:
sitename.ru/category/abc?letter=C&page=2
What regular expression is needed to be like this:
sitename.ru/category/abc/C/page/2

Answer the question

In order to leave comments, you need to log in

1 answer(s)
M
Maxim Fedorov, 2016-11-23
@DangerPro

Use Named Parameters in URL Rules

'urlManager' => [
    'enablePrettyUrl' => true,
    'showScriptName' => false,
    'rules' => [
        // порядок следования правил важен для корректного формирования ссылок
        'category/abc/<letter:\w+>/page/<page:\d+>' => 'category/abc',
        'category/abc/<letter:\w+>' => 'category/abc',                
    ]
],

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question