I have UserControler
and I run it in virtual server http://basic.com/index.php?r=user/index. How can I set up UserController
and action index
to be the default when I go to http://basic.com
![](../../users/profiles/1168156.webp)
- 37,789
- 9
- 89
- 156
![](../../users/profiles/3787302.webp)
- 646
- 1
- 6
- 17
-
What have you tried? And did you google this: http://www.larryullman.com/2013/02/18/understanding-routes-in-the-yii-framework/ – Philip Pittle Sep 04 '14 at 13:04
9 Answers
![](../../users/profiles/1805836.webp)
- 1,979
- 18
- 17
-
How can i set default in Controller ?, ex, if login will another default router – dungphanxuan Mar 29 '18 at 08:03
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',
];
![](../../users/profiles/1621821.webp)
- 8,090
- 8
- 51
- 72
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 :)
![](../../users/profiles/1168156.webp)
- 37,789
- 9
- 89
- 156
Open the web.php
file from your configuration folder, and add the following:
'defaultRoute' => 'admin'
to your $config
array.
![](../../users/profiles/2341603.webp)
- 36,816
- 9
- 39
- 58
![](../../users/profiles/6144679.webp)
- 31
- 2
This is not the answer of question, however it might be helpful to know:
there is a catchAll
property in yii\web\Application
which is defined as:
The configuration specifying a controller action which should handle all user requests.
Usage:
'catchAll' => ['controller/action']
So, every request to http://basic.com will call controller/action
![](../../users/profiles/713916.webp)
- 13,704
- 3
- 57
- 61
Note that defaultRoute
is ignored, when strictParsing
is set to enabled in the urlManager
config. Refer to the issue on GitHub: https://github.com/yiisoft/yii2/issues/5892
The following config is recommended as a measure:
[
...
'defaultRoute' => 'default/index',
...
'urlManager' => [
'enablePrettyUrl' => true,
'enableStrictParsing' => true,
...
'rules' => [
...
'' => '', // <- this line should be added
],
],
...
]
![](../../users/profiles/1769384.webp)
- 746
- 1
- 9
- 21
With yii2 I was able to do it in the urlmanager with:
'rules'=> [
['pattern'=>'<action>', 'route'=>'controller/<action>'],
]
![](../../users/profiles/2084693.webp)
- 670
- 8
- 17
Try the other solutions... If they do not work, then use my simple trick...
Just create an index.php page at the root.
Then, in that file, write this code:
return header('Location: http://your page location');
![](../../users/profiles/409156.webp)
- 5,123
- 5
- 32
- 44
![](../../users/profiles/7192864.webp)
- 11
- 3
If 'defaultRoute' don't work - check settings for 'urlManager'. Maybe default route is set there.
'urlManager' => [
'enablePrettyUrl' => true,
'showScriptName' => false,
'rules' => [
'' => 'site/index', // this line should be chenged to ''=>''.
'<controller:\w+>/<action:\w+>/' => '<controller>/<action>',
],
],
![](../../users/profiles/12026519.webp)
- 1