Pragmatism in the real world

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.

Using PHP's NumberFormatter to format currencies

I've been using number_format() for a very long time, but recently discovered that within the intl extension there's a NumberFormatter class available too. This is quite a clever class as it is Locale aware and handles formatting currency, including the correct symbol. You can check if you have the intl extension installed using php -m | grep intl and if you don't then you can install it with apt-get install php5-intl or yum install php-intl… 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.

Zend Framework Training

I provide in-house ZF1 training and ZF2 training through my company, Nineteen Feet. My 1, 2 or 3 day training courses will help your team learn and use Zend Framework and make your projects a success. Please email me for further information.

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.

NomadPHP 2013: Introduction to ZF2

Zend Framework 2 has matured nicely over the last 6 months, so this talk looked at how it works! In this talk, I walked through the structure of a ZF 2 application. I covered configuration, service location, modules, events, and the MVC system to provide a clear introduction to the key elements of a Zend Framework 2 application.

php|tek 2013: Zend Framework 2 Tutorial

This half-day tutorial introduced ZF2's MVC including the foundation concepts of service manager and event manager. We also looked at routing, bootstrapping, controllers and views.

Injecting configuration into a ZF2 controller

One thing you may find yourself needing to do is access configuration information in a controller or service class. The easiest way to do this is to use the ServiceManger's initialiser feature. This allows you to write one piece of injection code that can be applied to multiple objects. It's easier to show this in action! Let's assume that we have this configuration file: config/autoload/global.php: return array( 'application' => array( 'setting_1' => 234, ) );… continue reading.

Simple logging of ZF2 exceptions

I recently had a problem with a ZF2 based website where users were reporting seeing the error page displayed, but I couldn't reproduce in testing. To find this problem I decided to log every exception to a file so I could then go back and work out what was happening. In a standard ZF2 application, the easiest way to do this is to add a listener to the 'dispatch.error' event and log using ZendLog. To… continue reading.