Zend framework 2 Rest API : Calls getList() instead of get($id) function
Asked Answered
A

2

7

Following is my module config file

return array(
'controllers' => array(
    'invokables' => array(
        'RSMobile\Controller\User' => 'RSMobile\Controller\UserController',
    ),
),

// Routes for API calls
'router' => array(
    'routes' => array(

        'rsmobile' => array(
            'type' => 'segment',
            'options' => array(
                'route' => '/rsmobile',
                'defaults' => array(
                    'controller' => 'RSMobile\Controller\User',
                )
            ),

            // Child routes
            'child_routes' => array(
                // Route for "user" API
                'user' => array(
                    'type' => 'segment',
                    'options' => array(
                        'route' => '/user[/:id]',
                        'constraints' => array(
                                               'id'     => '[0-9a-zA-Z]+',
                                                              ),
                        'defaults' => array(
                            'controller' => 'RSMobile\Controller\User',
                        )
                    ),
                ),
          )

Question:

I have extends AbstractRestfulController in UserController file but, when i call this with www.example.com/rsmobile/user?userid=1 it call get-list instead of get.

Any Light on path would be helpful

Thanks

Astronaut answered 27/12, 2013 at 11:59 Comment(0)
E
4

I think you want to use www.example.com/rsmobile/user?userid=1 pattern only and not www.example.com/rsmobile/user/1.

In AbstractRestfulController, $identifierName is set to id, by default. If it does not find id in list of params then it will call getList() method. So what you can do is in your controller(which must be extending AbstractRestfulController) write below code :

public function __construct() {
    $this->identifierName = 'userId'; // Override $identifierName value specified in AbstractRestfulController.
}
Emera answered 31/12, 2013 at 6:19 Comment(0)
O
2

What I understand is that the request is matching your /rsmobile/ route instead of /rsmobile/user route. is that it?

I don't know how you deal with the paramater userid but probably, you don't need it, and instead of www.example.com/rsmobile/user?userid=1 you can call www.example.com/rsmobile/user/1 that will match your route, and will give you an id parameter in your controller.

Also, I think you are missing the 'may_terminate' => true int the child route. Probably you should be:

        // Child routes
        'child_routes' => array(
            // Route for "user" API
            'user' => array(
                'type' => 'segment',
                 'may_terminate' => true,
                'options' => array(
                    'route' => '/user[/:id]',
                    'constraints' => array(
                                           'id'     => '[0-9a-zA-Z]+',
                                                          ),
                    'defaults' => array(
                        'controller' => 'RSMobile\Controller\User',
                    )
                ),
            ),
Oology answered 30/12, 2013 at 20:24 Comment(2)
Actually i want to use /user?userId=1 instead of user/1 and when use that method it call's getList() and if call like /user?userId=1&id=0 then it call get() ,is it that we have to use 'id'?Astronaut
The answer of @akash is what you need!Oology

© 2022 - 2024 — McMap. All rights reserved.