How to access route, post, get etc. parameters in Zend Framework 2

前端 未结 5 1759
野性不改
野性不改 2020-11-27 10:06

How can I get various parameters related to the page request in zf2? Like post/get parameters, the route being accessed, headers sent and files uploaded.

5条回答
  •  青春惊慌失措
    2020-11-27 10:41

    The easisest way to get a posted json string, for example, is to read the contents of 'php://input' and then decode it. For example i had a simple Zend route:

    'save-json' => array(
    'type' => 'Zend\Mvc\Router\Http\Segment',
                'options' => array(
                    'route'    => '/save-json/',
                    'defaults' => array(
                        'controller' => 'CDB\Controller\Index',
                        'action'     => 'save-json',
                    ),
                ),
            ),
    

    and i wanted to post data to it using Angular's $http.post. The post was fine but the retrive method in Zend

    $this->params()->fromPost('paramname'); 
    

    didn't get anything in this case. So my solution was, after trying all kinds of methods like $_POST and the other methods stated above, to read from 'php://':

    $content = file_get_contents('php://input');
    print_r(json_decode($content));
    

    I got my json array in the end. Hope this helps.

提交回复
热议问题