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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-25 17:56:17  来源:igfitidea点击:

How to set the default controller in yii2

phpyiiyii2configdefault

提问by Hymanson Tong

I have UserControlerand I run it in virtual server http://basic.com/index.php?r=user/index. How can I set up UserControllerand action indexto 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

Did you try in your config:

您是否在配置中尝试过:

'defaultRoute' => 'user/index'

Default Controller

默认控制器

回答by d.raev

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

就像很少有人说过的那样,您需要defaultRoute在配置文件中添加。
这是它的外观:

//config/web.phpin basictemplate or backend/config/main.phpin 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 urlManagercomponent. Then if you want let's say domain/profileto behave like domain/user/profilethen these rules for urlManagermight 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.phpfile from your configuration folder, and add the following:

web.php从您的配置文件夹中打开该文件,并添加以下内容:

'defaultRoute' => 'admin'

'defaultRoute' => 'admin'

to your $configarray.

到您的$config阵列。

enter image description here

在此处输入图片说明

回答by chintogtokh

Note that defaultRouteis ignored, when strictParsingis set to enabled in the urlManagerconfig. Refer to the issue on GitHub: https://github.com/yiisoft/yii2/issues/5892

请注意,在配置中设置为启用defaultRoutestrictParsing会被忽略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 catchAllproperty in yii\web\Applicationwhich 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>',
        ],
    ],