zf2

Changing Layout in ZF2 Controller

November 15, 2015 by

Changing the layout or template within a Zend Framework 2 is easy. Thanks to the layout controller plugin, one can change the layout for a given controller action in just one line. public function indexAction() { $this->layout(‘layout/default’); return new ViewModel(); } As you can see above, the only thing you have to do in order… read more

Get Base Url within ZF2 Controller

November 15, 2015 by

Getting the base url of a request in Zend Framework 2 is quite simple. Because you are probably extending the AbstractActionController class, you get some functionality out of the box, including easily accessing the current request. From the request, one can get the URI, which exposes various methods for different parts of the URI. In… read more

Getting URL Parameters in ZF2 Controllers

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… read more

Creating a Custom ZF2 Controller Plugin

October 12, 2015 by

Creating a custom controller plugin in Zend Framework 2 is easy! First, begin by creating a new class. I will put this in the Application\Controller\Plugin namespace. namespace Application\Controller\Plugin; use Zend\Mvc\Controller\Plugin\AbstractPlugin; class HelloWorld extends AbstractPlugin { public function __invoke() { return $this->hello(); } public function hello() { return ‘Hello World!’; } } As you can see,… read more

ZF2: Accessing View Helpers in Controllers

July 4, 2014 by

Generally speaking, view helpers should not be used within controllers, as the name suggests. Certain situations may, however, require one to do so. This article shows you just how to do that by fetching the view helper manager from the service manager. It is actually very simple. public function someAction() { $userData = ‘User data… read more

Adding Query Parameters with ZF2 URL Helper

June 12, 2014 by

In a view script in Zend Framework 2, one may need to generate an URL for a given route that includes one or more query parameters. For instance, when browsing through a paginated data set, one may want a form’s action attribute to point to the current page, such that the user will end on… read more

Create Full URLs in ZF2 Views

March 10, 2013 by

Creating full URLs within view scripts can come in handy in many situations, such as when sending out e-mails where one wants to link back to one’s website. Luckily, Zend Framework 2 provides two useful view helpers to accomplish this task.