A
A
alysenko2016-08-18 21:29:39
Yii
alysenko, 2016-08-18 21:29:39

How to hide the name of the controller and action in Yii2?

How can I set up urlManager rules so that the name of the controller and action is not displayed in the address bar, but the ID of a specific post immediately goes, for example.
Let's say there is an address " example.com?r=site&a=action&id=2 ", but you need to do "example.com/2" .

Answer the question

In order to leave comments, you need to log in

2 answer(s)
M
Maxim Timofeev, 2016-08-18
@alysenko

'urlManager' => [
            'enablePrettyUrl' => true,
            'showScriptName' => false,
            'rules' => [
'<id>' => 'site/action'
            ],
        ],

D
Dmitry, 2016-08-18
@slo_nik

'urlManager' => [
            'enablePrettyUrl' => true,
            'showScriptName' => false,
            'rules' => [
                 '<id:\d+>' => '<_c>/<_a>'
                 //or
                '<id:\d+>' => 'site/<_a>'
               //or
               '<id:\d+>' => 'site/view'
              // и ещё другие варианты могут быть
            ],
        ],

In addition, you need to configure .htaccess

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question