'Zend framework 2 restful api not working for me

I am using zf2 restful api in my web services.

This is my code -

    module.config.php -

    'login' => array(
        'type'    => 'segment',
        'options' => array(
            'route'    => '/ws/login[/:id]',
            'defaults' => array(
                '__NAMESPACE__' => 'Webservices\Controller',
                'controller'    => 'Login',
            ),
        ),
    ), 

This is my controller -

   <?php

    namespace Webservices\Controller;

    use Zend\Mvc\Controller\AbstractRestfulController;
    use Zend\View\Model\JsonModel;

    class LoginController extends AbstractRestfulController {

        public function getList() {
            return new JsonModel(array(
                'data' => '',
            ));
        }

        /**
         * params time, language
         * listing category details
         * return category details
         */
        public function get($id) {
            return new JsonModel(array(
                'data' => '',
            ));
        }

        public function create($requestData) {
            print_r($requestData);
            die();
        }

    }

When I post some data into this controller then it redirects into create function. But requestData variable is NULL.

Raw data method is used for posting. This is my request data

     {"reqType":"2","verNo":"test","userName":"test==","deviceIdentifier":"DKZWcdvB50+test+test","password":"test=="}

For some technical reasons I am still using php 5.3.3 and zf2.0.



Solution 1:[1]

Sorry I don't have privileges to do comment your question so I just write this as an answer even if this is not.

  1. I'm not sure if your routing is well configured in module.config.php.
  2. There is no action defined in the routing.
  3. Also in your controller the name of the create function shouldn't be createAction ??
  4. I haven't tried but I guess if we're talking about actions in the controller then you're not able to use the parameter list of the action function (only if you defined them in your routing properly). Use instead the following: $this->params()->fromPost(parameterName)

If I misunderstood anything please let me know but I think this problem is not actually related with the zf2 restful API instead how to do routing in zf2 and how to get the parameters inside the actions.

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 geri