phprestzend-framework2zend-rest

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


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


Solution

  • 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.
    }