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.

Throw an exception when simplexml_load_string fails

I keep having to look up how to stop the warning that are emitted when simplexml_load_string & simplexml_load_file fail, so this time I've written the world's simplest little class to take care of it for me from now on: <?php namespace Rka; use UnexpectedValueException; class Xml { /** * Load an XML String and convert any warnings to an exception * * @param string $string * @param string $class_name * @param int $options * @param… continue reading.

Redirecting in Slim 2 Middleware

I recently ran into a problem with calling redirect() in a Slim Framework 2 middleware class, so I thought I'd better document the solution so that I remember in future! The authentication middleware looks roughly like this: class Authentication extends \Slim\Middleware { public function call() { if (!$this->isLoggedIn()) { $app = \Slim\Slim::getInstance(); $app->redirect($app->urlFor('login')); } $this->next->call(); } // other methods… } The problem is that you can't call $app->redirect() in Middleware as redirect() is designed to… 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.

View status of all Vagrant environments

I've just upgraded to Vagrant version 1.6, and vagrant global-status is possibly my favourite new feature. This command lists all currently up Vagrant environments wherever they may be on your computer: $ vagrant global-status id name provider state directory ————————————————————————————– dbc7770 joindin virtualbox running /Users/rob/www/thirdparty/joindin-vm 0683c7a default virtualbox running /Users/rob/www/thirdparty/joindin-zs7 The above shows information about all known Vagrant environments on this machine. This data is cached and may not be completely up-to-date. To interact with… continue reading.

Prefixing every key in a hash ref in Perl

I needed to prepend some text to every element in a Perl hash ref, so I came up with: $hashref->{"prefix_$_"} = delete($hashref->{$_}) foreach (keys %$hashref); which prefixes each key of $hashref with "prefix_". After talking it over with Cliff, he was concerned with modifying the list in place while simultaneously iterating over it and suggested this solution: %$hashref = map { +"prefix_$_" => $hashref->{$_} } keys %$hashref; One interesting thing about this solution is the… continue reading.

Z-Ray for Zend Server 7

I see that Zend Server 7 has now been released. I've been running the beta for all my development work for a while now and the main reason is the new Z-Ray feature. Z-Ray is a bar that is injected into the bottom of your page showing lots of useful information. This is what it looks like in its closed state when run on my development version of joind.in: At a glance, I can see… continue reading.

Privilege

If ever there was a word to make someone defensive, it's privilege. You are brought face to face with the fact that someone else doesn't have the same experiences in life as you and that their experiences somehow make their life harder than yours. I am one of the most privileged people I know. I am a well-educated European white man. I was raised in a loving, stable home. I have a loving, stable relationship… continue reading.