yii\rest\UrlRule with "pluralize" rule is not working in Yii2 REST

513 Views Asked by At

I am unable to access default endpoints with pluralize option, view action is also not working

Case 1: Access without configure Module

'controllerNamespace' => 'api\controllers',

...

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

http://localhost/api/web/countries Not working

http://localhost/api/web/country is working fine

http://localhost/api/web/country/1 is Not working

Case 2: Access via module 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' is not working completely and when access
v1/country & v1/country/12 both giving same result as index action (country list)

1

There are 1 best solutions below

5
Muhammad Omer Aslam On

Your rule is incorrect you are missing the module name v1 in your rules

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

Now you can access it with

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

Note : in order to enable the POST request along with GET add it to the extra patterns like 'GET,POST index' => 'index',