M
M
muhammadkhon2017-08-10 11:50:46
Yii
muhammadkhon, 2017-08-10 11:50:46

How to remove names of all controllers from url in Yii2?

For example, there are User, Site, etc. controllers. It is necessary that during the transition the name of the controller was not displayed in the url.
Instead of:
site.com/user/auth
Was:
site.com/auth
__________________________________________
Instead of:
site.com/site/contact
Was:
site.com/contact

Answer the question

In order to leave comments, you need to log in

2 answer(s)
M
Maxim Fedorov, 2017-08-10
@muhammadkhon

I will supplement the answer from Ilya Karavaev
All this is configured in the urlManager configuration, for example, if All requests in which there is no controller must be redirected to SiteController, then this can be done like this:

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

if you want to address the actions of each controller without their name, then for each action you need to write your own rule, for example:
'urlManager' => [            
    'enablePrettyUrl' => true,
    'showScriptName' => false,
    'rules' => [                
        '/contact' => 'site/contact', 
        '/auth' => 'user/auth'
    ]
],

You can read more details here

I
Ilya Karavaev, 2017-08-10
@Quieteroks

Via urlManager. You just need to make the right list of rules (rule). And you will most likely have to do this for every such shorthand, because you need to explicitly specify where to send the request to Yii itself anyway.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question