Learning ZF2: The Controller

Head's up! This post was written back in 2012 and is very likely to contain outdated information.

When Zend Framework 2 was officially released, I wanted to learn it on practical example.
So, I decided to use it as a foundation for my new blog engine. Because it is very simple
application (just a few classes), I was able to build it pretty easily.

This is first post of short series where I will describe how common problems are solved in ZF2.
I will always compare it with similar code written in ZF1-style.

First I'm going to describe some common tasks that you usually do in your controllers: handling
input parameters and accessing application services.

Parameters

There are few different types of parameters that you may want to access from controller:

route parameters (defined in router config)

query parameters (what you'd normally access via $_GET)

post parameters ($_POST)

Before getting to actual code, let's assume we have following route defined:

/view-album/:artist/:album_name

And that user has following in his address bar:

/view/album/Nirvana/Nevermind?view_details=1

In Zend Framework 1 all parameters listed above are available through
Zend_Controller_Request_Http class. Here's how it can be used from controller:

In Zend Framework 2 you have to be more explicit on what you want to retrieve.
Request doesn't let you retrieve route parameters anymore
(route params are part of router logic, they are not defined in HTTP request) - you need to
use RouteMatch object.
Code below shows how to access this data in ZF2:

Accessing dependencies

Zend Framework 1 didn't come with decent dependency system. Different programmers used different solutions
to manage relations between objects:

public function indexAction()
{
// some objects will take care of their dependencies by themselves
$userService = new App_Service_User();
// sometimes services were pulled from Bootstrap object...
$db = $this->getInvokeArg('bootstrap')->getResource('db');
// ... or from registry:
$translator = Zend_Registry::get('Zend_Translate');
}

Zend Framework 2 comes with ServiceManager - dedicated class that takes care of service creation and serves as
object container. The thing that makes it different from registry or
Zend Bootstrap
is that it creates objects only when they are needed. It brings significant improvement to loading speed - you don't
have to initialize mail transport and database only to display static page.