Pragmatism in the real world

2017 in pictures

Another year has passed which gives me an excuse to to reflect on what's happened. As usual, I look at the photos that I've taken and frame my thoughts around them. January I started the year with one of my favourite pictures of the kids. I also spoke at CodeMash in Ohio. This was a new conference to me and I knew no-one there. Fortunately, I was introduced to Mo, so had at least one… continue reading.

Run the UniFi Controller headless on Mac

I'm running a UniFi network here with wireless access points, the Security Gateway and a PoE switch. It seems to be a robust system and is almost certainly overkill, but reliability is high on my lists after bad experiences with a NetGear WiFi router. The UniFi system software is called the Controller and runs on a various operating systems. As I have a Mac mini here, I decided to run it on there. Weirdly, however… continue reading.

Generating training handouts for printing

When training, I like to provide my attendees with a printed copy of the slides with space next to each one to write notes. This handout has 4 slides to a page, along with a title page and I use pdfjam to create it. Pdfjam is a wonderful command line tool for manipulating PDFs and fits the bill perfectly for converting a PDF with one slide per page into a PDF with 4 slides per… continue reading.

Implementing CORS in Zend Expressive

On a recent project, I needed to implement CORS support for my Expressive API. The easiest way to do this is to use Mike Tuupola's PSR-7 CORS Middleware. As this is a standard Slim-Style PSR-7 middleware implementation, we need to wrap it for Expressive, so we make a factory: App/Factory/CorsMiddlewareFactory.php: <?php declare(strict_types=1); namespace App\Factory; use Tuupola\Middleware\Cors; use Zend\Diactoros\Response; use Zend\Stratigility\Middleware\CallableMiddlewareWrapper; class CorsMiddlewareFactory { public function __invoke($container) { return new CallableMiddlewareWrapper( new Cors([ "origin" => ["*"],… continue reading.

Customising Whoops in Expressive

I find the Whoops error handler page in Expressive quite hard to read and particularly dislike that the error message displayed in the top left is hidden if it's more than a few words long. To fix this, I discovered that you can provide a custom CSS file to the PrettyPrintHandler and then override to your heart's content! One way to do this is to add a delegator factory to add the additional functionality, so… continue reading.

Using Docker to create a MySQL server

When working on test code on my computer, I usually use the built-in PHP server (php -S) which works nicely. Every so often, I need access to MySQL and I use Docker to temporarily create a MySQL server for me. This is how I do it. The magic command is: $ docker run –name mysql \ -e MYSQL_USER=rob -e MYSQL_PASSWORD=123456 -e MYSQL_DATABASE=bookshelf \ -p 3306:3306 -d mysql/mysql-server:5.7 This creates a Docker container called "mysql" on… continue reading.

OuputBuffer Middleware for Expressive

When developing an Expressive application, if you use var_dump(), echo, print_r(), etc when you get this error: Fatal error: Uncaught RuntimeException: Output has been emitted previously; cannot emit response in /www/dev/rka/example-app/vendor/zendframework/zend-diactoros/src/Response/SapiEmitterTrait.php:31 This occurs after your data has been displayed and makes perfect sense as Expressive is unable to send headers as you've already started sending the body. To solve this, you need some middleware. It looks like this: <?php namespace Akrabat\Middleware; use const Webimpress\HttpMiddlewareCompatibility\HANDLER_METHOD; use… continue reading.

Displaying errors in Expressive with Twig

If you're not using the Whoops error handler with Expressive and are using the Twig renderer, then you are given no information about the problem that occurred, even in debug mode. To fix this, I changed error.html.twig to this: {% extends '@layout/default.html.twig' %} {% block title %}{{ status }} {{ reason }}{% endblock %} {% block content %} <h1>Oops!</h1> <h2>This is awkward.</h2> <p>We encountered a {{ status }} {{ reason }} error.</p> {% if status… continue reading.

"Software architecture is failing" by Alex Hudson

Alex Hudson's article, "Software architecture is failing" is a really interesting article and resonates with me as I come across this a lot. I recommend reading it. I'm very much in the camp of designing for the business requirements first and my biggest successes are the apps which solve my clients' problems. To call one of my apps "legacy" is a compliment; it means that I've written something that works and is solving real-problems for… continue reading.

Pretty print curl -i

My favourite tool for working with APIs is curl, though I recognise that lots of people like HTTPie and seem very keen to tell about it every time I mention curl… With curl, I particularly like using the -i switch to view the status line and headers too without the additional cruft of -v: This generates an output that looks like this: $ curl -i https://api.joind.in HTTP/1.1 200 OK Date: Wed, 04 Oct 2017 09:51:46… continue reading.