1

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)

Muhammad Omer Aslam
  • 22,976
  • 9
  • 42
  • 68
Ravee Sunshine
  • 386
  • 1
  • 5
  • 15

1 Answers1

1

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',

Muhammad Omer Aslam
  • 22,976
  • 9
  • 42
  • 68