Search code examples
zend-framework3

ZF3 development of RestAPI: handling Post request for


I am new to Zf3, and developing Email Restful API.

having difficulties in handling request. I can't get POST parameters.i think there is some problem in module.config.php > 'router'

Questions

1 How can I get POST variables from request.

2 I found it when I request page (using Postman)

    - if I passes ID, controller call get($id) method
    - if I POST variables controller calls Create($data) function.

      --is this always the case ? and is it good to write zendEmail code inside create($data) method .



bwsmail controller

(create function)

    public function create($data)  
    {
        echo $this->getRequest()->getPost('id', "no value");
              // Output
              // this returns no value

        echo $this->getRequest();
              // Output
              //POST http://localhost:8080/bwsmail/mail?id=123456789 HTTP/1.1
              //Cookie: PHPSESSID=p5i7o8lm2ed0iocdhkc8jvos01
              //Cache-Control: no-cache
              //Postman-Token: ccaf5b37-02a2-4537-bf3f-c1dc419d8ceb
              //User-Agent: PostmanRuntime/7.6.1
              //Accept: */*
              //Host: localhost:8080
              //Accept-Encoding: gzip, deflate
              //Content-Length: 0
              //Connection: keep-alive


         $response = $this->getResponseWithHeader()->setContent( __METHOD__.' create new item of data :<b>'.'</b>');
         return $response;
}

module.config.php (route)

'bwsmail' => [
            'type' => Literal::class,
            'options' => [
                'route' => '/bwsmail',
                'defaults' => [
                    'controller' => Controller\BwsMailController::class,
                ]
            ],
            'may_terminate' => true,
            'child_routes' => [
                                'mail' => [
                                    'type' => 'segment',
                                    'options' => [
                                        'route' => '/mail[/:id]',
                                        'constraints'=>
                                        [
                                            'id' => '[0-9a-zA-Z]+',
                                        ],
                                        'defaults' => [
                                            'controller' => Controller\BwsMailController::class,
                                        ]
                                    ],
                                ],
                            ],
             ],

Solution

  • if BwsMailController extends AbstractController or AbstractRestfulController you can simply: $this->params()->fromRoute() or ->fromQuery() if you're using a get string. For example:

    $routeParamId = $this->params()->fromRoute('id', null);
    

    This gets the parameter id as it is defined in the route configuration. If it's not set, it will default to null.

    If you have a GET url, something like: site.com/mail?id=123, then you do:

    $getParamId = $this->params()->fromQuery('id', null); // sets '123' in var
    

    More options are available, have a good read of the routing documentation.