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.
Share
Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
Please briefly explain why you feel this question should be reported.
Please briefly explain why you feel this answer should be reported.
Please briefly explain why you feel this user should be reported.
The easiest way to do that would be to use the Params plugin, introduced in beta5. It has utility methods to make it easy to access different types of parameters. As always, reading the tests can prove valuable to understand how something is supposed to be used.
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:
Default values
All of these methods also support default values that will be returned if no parameter with the given name is found.
Example:
When visiting http://example.com/?orderby=birthdate,
$orderBy will have the value birthdate.
When visiting http://example.com/,
$orderBy will have the default value 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:
When visiting http://example.com/?orderby=birthdate&filter=hasphone $allGetValues will be an array like
Not using Params plugin
If you check the source code for the Params plugin, you will see that it’s just a thin wrapper around other controllers to allow for more consistent parameter retrieval. If you for some reason want/need to access them directly, you can see in the source code how it’s done.
Example:
NOTE: You could have used the superglobals $_GET, $_POST etc., but that is discouraged.