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

Brad Johnson. asked, Aug 08 ' 2015

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

  • zend-framework2
  • POST
  • GET
  • routes
  • parameters

1 Answers

Get a single value

To get the value of a named parameter in a controller, you will need to select the appropriate method for the type of parameter you are looking 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 also support default values that will be returned if no parameter with the given name is found.

Example:

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

 

Get all parameters

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

Example:

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

 

Leave a Ansewer