[ACCEPTED]-How to access route, post, get etc. parameters in Zend Framework 2-zend-framework2

Accepted answer
Score: 206

The easiest way to do that would be to use 26 the Params plugin, introduced in beta5. It has utility 25 methods to make it easy to access different 24 types of parameters. As always, reading 23 the tests can prove valuable to understand how something 22 is supposed to be used.

Get a single value

To get the value 21 of a named parameter in a controller, you 20 will need to select the appropriate method 19 for the type of parameter you are looking 18 for and pass in the name.

Examples:

$this->params()->fromPost('paramname');   // From POST
$this->params()->fromQuery('paramname');  // From GET
$this->params()->fromRoute('paramname');  // From RouteMatch
$this->params()->fromHeader('paramname'); // From header
$this->params()->fromFiles('paramname');  // From file being uploaded

 

Default values

All of these methods 17 also support default values that will be 16 returned if no parameter with the given 15 name is found.

Example:

$orderBy = $this->params()->fromQuery('orderby', 'name');

When visiting http://example.com/?orderby=birthdate, $orderBy will have 14 the value birthdate.
When visiting http://example.com/, $orderBy will have the 13 default value name.
 

Get all parameters

To get all parameters of one type, just 12 don't pass in anything and the Params plugin 11 will return an array of values with their 10 names as keys.

Example:

$allGetValues = $this->params()->fromQuery(); // empty method call

When visiting http://example.com/?orderby=birthdate&filter=hasphone $allGetValues will be an 9 array like

array(
    'orderby' => 'birthdate',
    'filter'  => 'hasphone',
);

 

Not using Params plugin

If you check the source code for the Params 8 plugin, you will see that it's just a thin 7 wrapper around other controllers to allow 6 for more consistent parameter retrieval. If 5 you for some reason want/need to access 4 them directly, you can see in the source 3 code how it's done.

Example:

$this->getRequest()->getRequest('name', 'default');
$this->getEvent()->getRouteMatch()->getParam('name', 'default');

NOTE: You could have used 2 the superglobals $_GET, $_POST etc., but 1 that is discouraged.

Score: 4

The easisest way to get a posted json string, for 10 example, is to read the contents of 'php://input' and 9 then decode it. For example i had a simple 8 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 7 it using Angular's $http.post. The post 6 was fine but the retrive method in Zend 5

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

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

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

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

Score: 1
require_once 'lib/Zend/Loader/StandardAutoloader.php';
$loader = new Zend\Loader\StandardAutoloader(array('autoregister_zf' => true));

$loader->registerNamespace('Http\PhpEnvironment', 'lib/Zend/Http'); 

// Register with spl_autoload:
$loader->register();

$a = new Zend\Http\PhpEnvironment\Request();
print_r($a->getQuery()->get()); exit;

0

Score: 0

If You have no access to plugin for instance 2 outside of controller You can get params 1 from servicelocator like this

//from POST
$foo = $this->serviceLocator->get('request')->getPost('foo'); 
//from GET
$foo = $this->serviceLocator->get('request')->getQuery()->foo;
//from route
$foo = $this->serviceLocator->get('application')->getMvcEvent()->getRouteMatch()->getParam('foo');
Score: 0

All the above methods will work fine if 5 your content-type is "application/-www-form-urlencoded". But 4 if your content-type is "application/json" then 3 you will have to do the following:

$params 2 = json_decode(file_get_contents('php://input'), true); print_r($params);

Reason 1 : See #7 in https://www.toptal.com/php/10-most-common-mistakes-php-programmers-make

More Related questions