Unexpected REST API behaviour

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();

    }

}

I am not sure why your resource names are not mapped as plurals, but your second problem is related to the wrong regex pattern.

Should not you check for numbers instead of word/chars?




'tokens' => [

    '{id}' => '<id:\\d+>' // <---------------

],



Thanks, I wasn’t really paying much attention to that side of things until I could figure out the pluralization issue.

Finally got this one figured out, I think it’s very much so an edge case but I’ll put the solution up anyway just in case someone else makes the same mistake in the future.

I had a urlManager in my .../app/common/config/main.php with a bunch of rules in there that applied to the other sections of the application. It made sense to use a urlManager in the common folder for DRYness but having one in the api config and one in the common config clearly caused a conflict. Removing the urlManager from common and instead having a separate one for each section solved the problem.