Developing software in the Real World

Creating a ZF2 form from config

I have a requirement to create a Zend\Form from a dynamically created array which means that I can't use the FormAbstractServiceFactory and so will use Zend\Form\Factory directly. If you need to override any form elements, validators or add new ones, then you'll need the correct plugin managers for the factory. The way to set this up is like this:

Your $factory is now configured and so you can create a form like this:

Injecting dependencies into your ZF2 controllers

When starting working with Zend Framework 2, it's common to copy the skeleton and put your controller definitions in module.config.php like this:

The controllers keyword is picked up by the ControllerManager which is an instance of the ServiceManager which means that it creates the controller instance for you when the dispatcher needs it.

Implementing a ZF2 development mode

One feature that piqued my interested in the Apigility skeleton application was development mode. From the README: Once you have the basic installation, you need to put it in development mode:

Investigating Apigility

At ZendCon 2013, Zend announced Apigility which is intended to ease the creation of APIs. It consists of these things: A set of ZF2 modules that do the heavy lifting of creating an API A application wrapper for creating standalone web API applications A built-in administration website for use in development to define the API Rather nicely, it supports REST and RPC and deal with error handling, versioning & content negotiation for you. Getting started… continue reading.

Returning JSON errors in a ZF2 application

If you have a standard ZF2 application and accept application/json requests in addition to application/html, then you have probably noticed that when an error happens, HTML is created, even though the client has requested JSON. One way to fix this is to create a listener on MVC's render event to detect that an error has occurred and substitute a JsonModel in place of the ViewModel. The easiest way to do this in your ApplicationModule. Firstly,… continue reading.

Configuring a ZF2 view helper before rendering

The currencyFormat view helper is very easy to use:

When I was reading the documentation for the currencyFormat view helper, I discovered that you could configure the currency code and locale once rather than in every call:

This is obviously useful, but even more useful would be if we could set it once by default and then override if we need to in a specific call. The easiest way to do this is… continue reading.

Returning a ZF2 HydratingResultSet when starting with raw SQL

If you're using Zend Framework 2's Zend\Db and want to write raw SQL that returns a HydratingResultSet, then you can do this:

When you iterate over the HydratingResultSet, you get a populated instance of MyEntity. The $hydrator instance is the object that knows how to populate your entity object. In my case, I've used an ArraySerializable hydrator, but there's other options available, or you can write your own. As an aside, if you don't… continue reading.

Displaying the generated SQL from a ZendDbSql object

If you use ZendDbSql to generate your SQL, then it's useful to find out what the generated SQL looks like. Consider code like this:

To find out what the generated SQL will look like, you can use the $select's getSqlString() method:

For me, this results in lots of warnings along the lines of: This is less than helpful, so to avoid the warnings, you need to supply the correct platform information to the… continue reading.

IN and Zend\Db\Sql's where()

This is a short note to myself. Zend\Db\Sql objects allow you to do this:

which generates the (My)SQL:

If you want the SQL generated to use the IN operator, then just pass in an array:

which generates:

This obviously also works for ZendDbSql's update() as well as select().

Caching your ZF2 merged configuration

Zend Framework 2's ModuleManager has the ability to cache the merged configuration information for your application. This is very useful as it allows you to separate out your configuration within the config/autoload directory into logical files without worrying about the performance implications of lots of files. Enabling caching is simply a case of setting these configuration keys in config/application.config.php within the module_listener_options section:

This then creates the cache files data/cache/module-classmap-cache.php and data/cache/module-config-cache.php and you're… continue reading.