Pragmatism in the real world

Returning JSON from a ZF2 controller action

The new view layer in Zend Framework 2 can be set up to return JSON rather than rendered HTML relatively easily. There are two steps to this: Set up the JsonStrategy Firstly we need to set up the view's JsonStrategy to check to a situation when returning JSON is required and then to render out JSON for us. The JsonStrategy will cause the JsonRenderer to be run in two situations: The view model returned by… continue reading.

A list of ZF2 events

Both the Module Manager and the MVC system use the Event Manger extensively in order to provide "hook points" for you to add your own code into the application flow. This is a list of the events triggered by each class during a standard request with the Skeleton Application: Module Manager Zend\Module\Manager: loadModules.pre For every module: Zend\Module\Manager: loadModule.resolve Zend\Module\Manager: loadModule Zend\Module\Manager: loadModules.post Application Successful: Zend\Mvc\Application: bootstrap Zend\Mvc\Application: route Zend\Mvc\Application: dispatch Zend\Mvc\Controller\ActionController: dispatch (if controller extends… continue reading.

Some Zend\View examples

With the release of Beta 3 of Zend Framework, we now have a significantly refactored the ZendView component. One of the changes made is that there is a ViewModel object that is returned from a controller which contains the variables to be used within the view script along with meta information such as the view script to render. The really nice thing about ViewModels is that they can be nested and this is how the… continue reading.

Module specific bootstrapping in ZF2

Update As of Beta 4, this method no longer works. Evan Coury's post on Module-specific layouts in Zend Framework 2 is the correct way to do this. Following on from the discussion on modules, we can hook into the event system to do module specific bootstrapping. By this, I mean, if you have some code that you want to run only if the action to be called is within this module, you can hook into… continue reading.

Modules in ZF2

A Zend Framework 2 application is made up of a number of modules which each contain the controllers, models, views and other support code required to implement a specific part of the application. Within a standard ZF2 application, modules live in one of two places: /module – for application specific modules /vendor – for 3rd party modules It follows that you are not expected to ever modify 3rd party modules that are stored in /vendor.… continue reading.

An introduction to Zend\Di

Zend Framework 2 provides its own dependency injection container, Zend\Di which is a key underpinning of the entire framework and especially the MVC system. I have covered before, my thoughts on the reasons for using dependency injection, so this article looks at the fundamentals of using Zend\Di. Constructor injection Consider this code: namespace My; class DatabaseAdapter { } class UserTable { protected $db; public function __construct (DatabaseAdapter $db) { $this->db = $db; } } This… continue reading.

Overriding module configuration in ZF2

Let's say that you install the ZF-Common's User module. By default, it sets up its routes under the /user path segment like this: vendor/ZfcUser/config/module.config.php return array( // lots of config stuff here /** * Routes */ 'ZendMvcRouterRouteStack' => array( 'parameters' => array( 'routes' => array( 'zfcuser' => array( 'type' => 'Literal', 'priority' => 1000, 'options' => array( 'route' => '/user', 'defaults' => array( 'controller' => 'zfcuser', 'action' => 'index', ), ), 'may_terminate' => true, 'child_routes'… continue reading.

What problem does dependency injection solve?

Zend Framework 2 comes with a dependency injection container (DIC), as does Symfony 2 and Aura, along with many other PHP frameworks that target PHP 5.3 or higher nowadays. This article attempts to explore the problem that a DIC tries to solve. Consider this simple (contrived!) example of an Album object that has an Artist object: class Album { protected $artist; public function getArtistName() { return $artist->getName(); } } The question is how to we… continue reading.

Using Zend\Loader\Autoloader

Autoloading is the process in PHP whereby the system attempts to load a class when it is first encountered (via new or via class_exists) if it hasn't already been loaded via a require or include. Autoload works by looking for a method called __autoload or walking through any method registered with spl_autoload_register. Zend Framework 2 provides the Zend\Loader\Autoloader component for autoloading of Zend Framework and your own classes. What does it provide? The ZF2 autoloader… continue reading.

Zend Framework 2 Beta 2 released

Zend Framework 2, Beta 2 has been released! The key new features are: Refactored Mail component Refactored Cache component MVC updates Check out Matthew's blog post for the full details. I've also updated my tutorial. This is a good time to get involved, try it out and let us know what you like/dislike.