How to set the default controller in yii2 How to set the default controller in yii2 php php

How to set the default controller in yii2


Did you try in your config:

'defaultRoute' => 'user/index'

Default Controller


Like few people already said, you need to add defaultRoute in the configurations file.
Here is how it should look:

//config/web.php in basic template or backend/config/main.php in advanced

$config = [    ...    'components' => [        ...    ],    'params' => $params,    'defaultRoute' => 'user/index',];


This can be set within the config, see Default Controller:

[    'defaultRoute' => 'main',]

But note that this is closely related to routing, which can be completely customized by urlManager component. Then if you want let's say domain/profile to behave like domain/user/profile then these rules for urlManager might be another way to go:

'rules' => array(    '<action:\w+>' => 'user/<action>', // <-- use UserController by default    '<controller:\w+>/<id:\d+>' => '<controller>/view',    '<controller:\w+>/<action:\w+>/<id:\d+>' => '<controller>/<action>',    '<controller:\w+>/<action:\w+>' => '<controller>/<action>',),

Hopefully this will help someone :)