1

我无法使用复数选项访问默认端点,查看操作也不起作用

案例一: 不配置模块访问

'controllerNamespace' => 'api\controllers',

...

'rules' => [
    [
        'class' => 'yii\rest\UrlRule',
        'controller' => 'country',   
        'tokens' => [
             '{id}' => '<id:\\w+>'
        ],
        /*'pluralize'=>false,*/
    ],
]

http://localhost/api/web/countries 不工作

http://localhost/api/web/country工作正常

http://localhost/api/web/country/1工作

案例 2: 通过模块v1访问

'modules' => [
     'v1' => [
          'basePath' => '@app/modules/v1',
          'class' => 'api\modules\v1\Module'
     ]
],

...

'rules' => [
     [
          'class' => 'yii\rest\UrlRule', 
          'controller' => ['country' => 'v1/country'],
          'tokens' => [
               '{id}' => '<id:\\w+>'
          ],
     ],
]

'pluralize' 没有完全工作,当访问
v1/countryv1/country/12时,两者都给出与索引操作相同的结果(国家列表)

4

1 回答 1

1

v1您的规则不正确您在规则中缺少模块名称

[
     'class' => 'yii\rest\UrlRule',
     'controller' => 'v1/country',   
     'tokens' => [
          '{id}' => '<id:\\w+>'
     ],
     'extraPatterns' => [
          'GET index' => 'index',
     ],
],

现在您可以使用

http://localhost/api/web/v1/countries 

注意:为了启用POST请求GET并将其添加到额外的模式中,例如 'GET,POST index' => 'index',

于 2019-08-20T11:12:25.130 回答