I have an application built from the advanced template. I have added an API to the application following a similar approach to this guide. The API works to a certain extent but has some unexpected behaviour.
I am dealing with just one model (User) in my API. When I make a GET request to ../api/web/v1/users (plural) I get an error, if I make a request to ../api/web/v1/user (singular) I get all users returned. Similarly, if I make a request to ../api/web/v1/user/13 I still get all users returned. Something is clearly wrong but I can’t see any obvious issues
This is my layout:
api
-config
-modules
--v1
---controllers
---- UserController.php
---models
---- User.php
---Module.php
-runtime
-web
urlManager in main.php
'urlManager' => [
'enablePrettyUrl' => true,
'enableStrictParsing' => true,
'showScriptName' => false,
'rules' => [
[
'class' => 'yii\rest\UrlRule',
'controller' => 'v1/user',
'tokens' => [
'{id}' => '<id:\\w+>'
],
],
],
]
api/modules/v1/controllers/UserController.php
namespace api\modules\v1\controllers;
use yii\rest\ActiveController;
class UserController extends ActiveController
{
public $modelClass = 'api\modules\v1\models\User';
}
api/modules/v1/models/User.php
namespace api\modules\v1\models;
use \yii\db\ActiveRecord;
class User extends ActiveRecord
{
/**
* @inheritdoc
*/
public static function tableName()
{
return '{{%user}}';
}
/**
* @inheritdoc
*/
public static function primaryKey()
{
return ['id'];
}
/**
* Define rules for validation
*/
public function rules()
{
return [
[['id', 'email', 'address', 'name'], 'required'],
];
}
}
api/modules/v1/Module.php
namespace api\modules\v1;
class Module extends \yii\base\Module
{
public $controllerNamespace = 'api\modules\v1\controllers';
public function init()
{
parent::init();
}
}