php 如何在yii2中设置默认控制器
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/25666205/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to set the default controller in yii2
提问by Hymanson Tong
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
我有UserControler
并且我在虚拟服务器http://basic.com/index.php?r=user/index 中运行它。当我转到http://basic.com时,如何设置UserController
和操作index
为默认值
回答by Aivar
回答by d.raev
Like few people already said, you need to add defaultRoute
in the configurations file.
Here is how it should look:
就像很少有人说过的那样,您需要defaultRoute
在配置文件中添加。
这是它的外观:
//config/web.php
in basictemplate or backend/config/main.php
in advanced
//config/web.php
在基本的模板或backend/config/main.php
在先进
$config = [
...
'components' => [
...
],
'params' => $params,
'defaultRoute' => 'user/index',
];
回答by LihO
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:
但要注意,这与路由密切相关,可以完全由urlManager
组件自定义。然后,如果你想让我们说domain/profile
表现得像domain/user/profile
那么这些规则urlManager
可能是另一种方式:
'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 :)
希望这会帮助某人:)
回答by xdev
Open the web.php
file from your configuration folder, and add the following:
web.php
从您的配置文件夹中打开该文件,并添加以下内容:
'defaultRoute' => 'admin'
'defaultRoute' => 'admin'
to your $config
array.
到您的$config
阵列。
回答by chintogtokh
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
请注意,在配置中设置为启用defaultRoute
时strictParsing
会被忽略urlManager
。参考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
],
],
...
]
回答by Ali MasudianPour
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:
这不是 question 的答案,但是知道它可能会有所帮助:有一个catchAll
属性yii\web\Application
定义为:
The configuration specifying a controller action which should handle all user requests.
指定应处理所有用户请求的控制器操作的配置。
Usage:
用法:
'catchAll' => ['controller/action']
So, every request to http://basic.comwill call controller/action
因此,对http://basic.com 的每个请求 都会调用controller/action
回答by VeYroN
With yii2 I was able to do it in the urlmanager with:
使用 yii2,我可以在 urlmanager 中使用:
'rules'=> [
['pattern'=>'<action>', 'route'=>'controller/<action>'],
]
回答by manik mistry
Try the other solutions... If they do not work, then use my simple trick...
尝试其他解决方案...如果它们不起作用,请使用我的简单技巧...
Just create an index.phppage at the root.
只需在根目录创建一个index.php页面。
Then, in that file, write this code:
然后,在该文件中,编写以下代码:
return header('Location: http://your page location');
回答by DeN
If 'defaultRoute' don't work - check settings for 'urlManager'. Maybe default route is set there.
如果“defaultRoute”不起作用 - 检查“urlManager”的设置。也许那里设置了默认路由。
'urlManager' => [
'enablePrettyUrl' => true,
'showScriptName' => false,
'rules' => [
'' => 'site/index', // this line should be chenged to ''=>''.
'<controller:\w+>/<action:\w+>/' => '<controller>/<action>',
],
],