M
M
Maxim Lagoysky2017-07-19 16:37:40
Yii
Maxim Lagoysky, 2017-07-19 16:37:40

Url manager different rules for one controller?

There is a site with such pages.
https://mysite/site/category?id=1&cat=category_1
https://mysite/site/sub?id_sub=1&al=sub_category_5
as you can see they are in the same controller, you need to bring them to this form
https://mysite/ category_1?id=1
https://mysite/sub_category_5?id_sub=5
how to implement it? Need to write your own set of rules? Or is it possible somehow by standard means?

Answer the question

In order to leave comments, you need to log in

2 answer(s)
M
Maxim Timofeev, 2017-07-19
@webinar

'urlManager' => [
            'class' => 'yii\web\UrlManager',
            'enablePrettyUrl' => true,
            'showScriptName' => false,
            'rules' => [
              '/<id>/<id_sub>' => 'site/category' 
           ]
        ],

and there will be links mysite.ru/1/5
and in the category action it is already possible to analyze whether there is a sub or not. Why 2 actions for this?

M
Maxim Popov, 2017-07-21
@maxomato

You can try to use the default values ​​in the rules.
More about their use of Default Parameter Values

'urlManager' => [
    'enablePrettyUrl' => true,
    'showScriptName' => false,
    'rules' => [
        [
            'pattern' => 'category_1',
            'route' => '/site/category',
            'defaults' => ['cat' => 'category_1']
        ],
        [
            'pattern' => 'sub_category_5',
            'route' => '/site/sub',
            'defaults' => ['al' => 'sub_category_5']
        ],                                 
    ],
],

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question