Yii Framework Forum: Unexpected REST API behaviour - Yii Framework Forum

Jump to content

Page 1 of 1
  • You cannot start a new topic
  • You cannot reply to this topic

Unexpected REST API behaviour

#1 User is offline   rob_dev 

  • Newbie
  • Yii
  • Group: Members
  • Posts: 11
  • Joined: 27-September 17

Posted 06 December 2017 - 04:29 AM

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

0

#2 User is offline   alrazi 

  • Elite Member
  • Yii
  • Group: Moderators
  • Posts: 1,499
  • Joined: 08-August 12
  • Location:Durban, South Africa

Posted 06 December 2017 - 07:36 AM

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+>' // <---------------
],

0

#3 User is offline   rob_dev 

  • Newbie
  • Yii
  • Group: Members
  • Posts: 11
  • Joined: 27-September 17

Posted 06 December 2017 - 08:42 AM

View Postalrazi, on 06 December 2017 - 07:36 AM, said:

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.
0

#4 User is offline   rob_dev 

  • Newbie
  • Yii
  • Group: Members
  • Posts: 11
  • Joined: 27-September 17

Posted 06 December 2017 - 09:07 AM

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.
0

Share this topic:


Page 1 of 1
  • You cannot start a new topic
  • You cannot reply to this topic

1 User(s) are reading this topic
0 members, 1 guests, 0 anonymous users