Tuesday, 29 December 2015

Zend Framework 2 get params from URL

Zend Framework 2 get params from URL


Question: How to read the data from php://input?
$content = file_get_contents('php://input');
print_r(json_decode($content));



Question: How to get parameter value from GET without using Params plugin?
$parameterName1=$this->getRequest()->getRequest('parameterName2');
$parameterName2=$this->getRequest()->getRequest('parameterName2','Default Value');



Now with use of params plugins, you can get values easily from all type of request


Question: How to get parameter value from URL?
$parameterName1=$this->params()->fromQuery('parameterName1');
$parameterName2=$this->params()->fromQuery('parameterName2','Default Value');



Question: How to get all parameters value from URL?
$parameterArray=$this->params()->fromQuery();



Question: How to get parameter value from POST?
$parameterName1=$this->params()->fromPost('parameterName1');
$parameterName2=$this->params()->fromPost('parameterName2','Default Value');



Question: How to get all parameters value from POST?
$parameterArray=$this->params()->fromPost();



Question: How to get parameter value from header?
$parameterName1=$this->params()->fromHeader('parameterName1');
$parameterName2=$this->params()->fromHeader('parameterName2','Default Value');



Question: How to get parameter value from uploadedFile?
$parameterName1=$this->params()->fromFiles('parameterName1');
$parameterName2=$this->params()->fromFiles('parameterName2','Default Value');



Question: How to get parameter value from RouteMatch?
$parameterName1=$this->params()->fromRoute('parameterName1');
$parameterName2=$this->fromRoute()->fromFiles('parameterName2','Default Value');