Rest, urlManager and pluralize

I’m trying to learn about Rest, but in my test pluralizing seems doesn’t working.

This my web.php




	'modules' => [

    	'v1' => [

        	'basePath' => '@app/api/v1',

        	'class' => 'api\v1\Module'

    	],

	], 


    	'urlManager' => [

        	'enablePrettyUrl' => true,

        	'showScriptName' => false,

        	'enableStrictParsing' => false,

        	'rules' => [

            	[

                	'class' => 'yii\rest\UrlRule',

                	'pluralize' => true,

                	'controller' => [

                    	'v1/country',

                    	'v1/user'

                	],

                	],

        	],

    	],



Module.php




<?php

namespace api\v1;


class Module extends \yii\base\Module

{

	public $controllerNamespace = 'api\v1\controllers';


	public function init()

	{

    	parent::init();    	

	}

}



Contry.php model




<?php

namespace api\v1\models;

use \yii\db\ActiveRecord;


class Country extends ActiveRecord 

{

	/**

 	* @inheritdoc

 	*/

	public static function tableName()

	{

		return 'country';

	}


	/**

 	* @inheritdoc

 	*/

	public static function primaryKey()

	{

    	return ['code'];

	}


	/**

 	* Define rules for validation

 	*/

	public function rules()

	{

    	return [

        	[['code', 'name', 'population'], 'required']

    	];

	}   

}



CountryController.php




<?php


namespace api\v1\controllers;


use yii\rest\ActiveController;


{

	public $modelClass = 'app\api\v1\models\Country';	

}



Going to: http://mysite.com/countries

Not found (404)

Going to: http://mysite.com/country

<response>

<item>

<code>AU</code>

<name>Australia</name>

<population>18886000</population>

</item>

<item>

<code>…

I don’t know where I’m doing wrong…

Try this, it is working fine here.


'urlManager' => [

            'enablePrettyUrl' => true,

            'enableStrictParsing' => true,

            'showScriptName' => false,

            'rules' => [

                [

                    'class' => 'yii\rest\UrlRule',

                    'controller' => ['v1/user'],

                    'tokens' => [

                        '{id}' => '<id:\\w+>',

                    ],             

                

                ]

            ],        

        ]

Only difference i see there is that you disabled


enabledStrictParsing

Plus the code you posted for the controller is for the model.

Try and post the controller code, in case that doesn’t work.

Yes, you are right. I edited the post adding ContryController, but it was not the only my mistake. In first time I added CountryController in the controllers directory, then I forgotten to erase it. After remove it from there, leaving only in “api/v1/controllers”, the rest seems doesn’t work at all.

  • The configuration in web.cfg to use the controller out of the default folder seems doesn’t work (with my configuration, of course).

  • If I set enableStrictParsing to enable, the normal site functions doesn’t work too (Not found #404)

Then I solved, basically my problems were caused from wrongs paths.