urlManager 在 Yii2 中找不到 site/index

urlManager can't find site/index in Yii2

我刚刚在 Yii 2 中创建了一个基本应用模式。

我尝试测试 RESTFull api。

该站点在默认情况下正常运行,但是当我更改 urlManager 时

'urlManager' => [
    'enablePrettyUrl' => true,
    'enableStrictParsing' => true,
    'showScriptName' => false,

    'rules' => [
            ['class' => 'yii\rest\UrlRule', 'controller' => 'cliente'],
    ],

],

我只收到 404 错误。

我输了site/index!!!

有什么想法吗?

就是这个设置: 'enableStrictParsing' => true,

看这里:

https://github.com/yiisoft/yii2/blob/master/framework/web/UrlManager.php#L323

没有明确的 url 定义的 siteController 规则。

在此处阅读更多内容:

https://www.yiiframework.com/doc/guide/2.0/en/runtime-routing#url-rules

由于此设置,

site/index 操作不再有效:'enableStrictParsing' => true。来自 $enableStrictParsing documentation:

If strict parsing is enabled, the incoming requested URL must match at least one of the $rules in order to be treated as a valid request. Otherwise, the path info part of the request will be treated as the requested route.

因此,如果您启用此设置,则需要有匹配的 URL 规则来支持给定的 URL。在您的情况下,您只有一个 REST 端点规则,因此任何其他 URL 都不起作用。您应该禁用此设置或为主页添加规则:

'rules' => [
    '' => 'site/index',
    ['class' => 'yii\rest\UrlRule', 'controller' => 'cliente'],
],