Getting URL Parameters in ZF2 Controllers

Published on November 13, 2015 by

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();
}
Featured

Learn Zend Framework today!

Take an online course and become a ZF2 ninja!

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!
Zend Framework logo
Author avatar
Bo Andersen

About the Author

I am a back-end web developer with a passion for open source technologies. I have been a PHP developer for many years, and also have experience with Java and Spring Framework. I currently work full time as a lead developer. Apart from that, I also spend time on making online courses, so be sure to check those out!

Leave a Reply

Your e-mail address will not be published.