Getting URL Parameters in ZF2 Controllers
Accessing URL parameters from controllers in Zend Framework 2 is extremely easy thanks to the params controller plugin. This plugin exposes various methods that one can use to retrieve parameters from various sources of a request, including from the URL.
I will give examples of the various sources below.
Getting URL Parameters
To get URL parameters within Zend Framework 2 controllers, simply invoke the fromRoute or fromQuery methods on the params controller plugin. The fromRoute method should be used when you wish to retrieve a segment from a segment route for instance, while the fromQuery is used to fetch query parameters from the URL.
public function viewPostAction()
{
// Get segment from URL
$postId = $this->params()->fromRoute('postId'); // "postId" = segment
// Get query parameter
$postId = $this->params()->fromQuery('postId');
return new ViewModel();
}
Getting POST Parameter
To get a POST parameter from a request, simply use the fromPost method on the params controller plugin.
public function viewPostAction()
{
// Get POST parameter
$postId = $this->params()->fromPost('postId');
return new ViewModel();
}
Getting HTTP Headers
To get a given HTTP header within a Zend Framework 2 controller, simply use the fromHeader method on the params controller plugin.
public function viewPostAction()
{
// Get HTTP header
$userAgent = $this->params()->fromHeader('User-Agent');
return new ViewModel();
}
Getting Files From Requests
To fetch a file from the request, one can simply use the fromFiles method on the params controller plugin.
public function viewPostAction()
{
// Get file from request
$myFile = $this->params()->fromFiles('MyFile');
return new ViewModel();
}
Getting Parameters from any Source
Conveniently, one can also retrieve a parameter without specifying from where the parameter should be fetch. For instance, to retrieve a postId regardless of where it is present, simply do as below.
public function viewPostAction()
{
// Get parameter from any source
$postId = $this->params('postId');
return new ViewModel();
}
The above will fetch the value of a postId parameter if one is available within any of the request sources.
Specifying a Default Value
When fetching parameters, one can also specify a default value that should be returned if the parameter is not available. This could for instance be useful when paginating a set of items; if no page parameter is available, it should default to one. This avoids having to make conditional checks and keeps the code slightly more clean.
public function viewPostAction()
{
// Specifying default value when retrieving from any source
$this->params('page', 1);
// Specifying default value when retrieving from specific source
$this->params()->fromRoute('page', 1);
return new ViewModel();
}
Here is what you will learn:
- Understand the theory of Zend Framework in details
- How to implement an enterprise-ready architecture
- Develop professional applications in Zend Framework
- Proficiently work with databases in Zend Framework
- ... and much more!