Pragmatism in the real world

Sending attachments in multipart emails with Zend\Mail

I've written before about how to send an HTML email with a text alternative in Zend\Mail, but recently needed to send an attachment with my multipart email. With help from various sources on the Internet, this is how to do it. use Zend\Mail\Message; use Zend\Mime\Message as MimeMessage; use Zend\Mime\Part as MimePart; use Zend\Mime\Mime; use Zend\Mail\Transport\Sendmail; function sendEmail($to, $from, $subject, $html, $text, $attachments = null) { $message = new Message(); $message->addTo($to); $message->addFrom($from); $message->setSubject($subject); // HTML part… continue reading.

Integrating ZF2 forms into Slim

Let's say that you want to use Zend Framework 2's Form component outside of ZF2 itself. In this case, a Slim application. It turns out that Composer makes this quite easy, though there's quite a lot of code involved, so this is a long article. Start with a really simple Slim Application. index.php: require 'vendor/autoload.php'; $app = new \Slim\Slim(); $app->map('/', function () use ($app) { $app->render('home.php', array( 'form' => $form )); })->via('GET', 'POST'); $app->run(); templates/home.php:… continue reading.

Globally overriding validation messages for ZF2 forms

One thing that I always do when creating a Zend Framework 2 form is override the validation messages for a number of validators – EmailAddress in particular. I recently decided that I should probably sort this one out once and be done with it. Turns out that it's quite easy assuming that you use the FormElementManger to instantiate your forms. All that I need to do is create my own validator classes that extend the… continue reading.

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.