Pragmatism 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: $factory = new Factory(); $formElements = $this->serviceLocator->get('FormElementManager'); $factory->setFormElementManager($formElements); $inputFilters = $this->serviceLocator->get('InputFilterManager'); $factory->getInputFilterFactory()->setInputFilterManager($inputFilters); Your $factory is now… continue reading.

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: 'controllers' => array( 'invokables' => array( 'Application\Controller\Index' => 'Application\Controller\IndexController', 'Application\Controller\Blog' => 'Application\Controller\BlogController', ), ), 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: cd path/to/install php public/index.php development enable # put the skeleton 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: echo $this->currencyFormat($value, 'GBP', 'en_GB'); 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: // Within your view script $this->plugin("currencyformat")->setCurrencyCode("GBP")->setLocale("en_GB"); 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… 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: use Zend\Db\Adapter\AdapterAwareInterface; use Zend\Db\Adapter\Adapter; use Zend\Stdlib\Hydrator\ArraySerializable; use Zend\Db\ResultSet\HydratingResultSet; use Zend\Db\Adapter\DriverResultInterface; use MyEntity\MyEntity; // some sort of entity object class MyMapper implements AdapterAwareInterface { protected $dbAdapter; public function fetchRowsWithValue($fieldValue) { $sql = "SELECT * FROM my_table_name WHERE my_field_name = ? "; $params = array( $fieldValue, ); return $this->fetch($sql, $params); } protected function fetch($sql, $params… 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: public function fetchAllWithTitleSince($title, $since) { $sql = new Sql($this->dbAdapter); $select = $sql->select(); $select->from($this->tableName); $select->columns(array('id', 'title', 'url', 'date_updated')); $select->where->like('title', "%$title%"); $select->where->greaterThanOrEqualTo('date_created', date('Y-m-d', strtotime($since))); $statement = $this->dbAdapter->createStatement(); $select->prepareStatement($this->dbAdapter, $statement); return $statement->execute(); } To find out what the generated SQL will look like, you can use the $select's getSqlString() method: $select->getSqlString(); For me, this… 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: $id = 2; $select->where(array('id' => $id)); which generates the (My)SQL: WHERE `id` = '2' If you want the SQL generated to use the IN operator, then just pass in an array: $idList = array(1, 3, 4); $select->where(array('id' => $idList)); which generates: WHERE `id` IN ('1', '3', '4') 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: 'module_listener_options' => array( 'config_cache_enabled' => true, 'module_map_cache_enabled' => true, 'cache_dir' => 'data/cache/',… continue reading.