Pragmatism in the real world

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.

Converting a Composer dependency to git for editing

I'm adding a new feature to ZF's Problem-Details component and it's easiest to do this within the context of the application I'm developing. The component lives in vendor/zendframework/zend-problem-details and was installed using composer require so doesn't have its own git repository as the distribution zip file was used to install it. To change it to a git repository, we can use the –prefer-source option and specify a branch like this: $ composer require –prefer-source zendframework/zend-problem-details:dev-master… continue reading.

Custom URLs for a Cloud Foundry application

Now that I have my application deployed to Cloud Foundry, I need it to work with my own domain, such as bookshelf.akrabat.com rather than slim-bookshelf.eu-gb.mybluemix.net. We can do this via the command line which is useful. To do this we need to create a domain and then we can create a route. Create a domain To create a domain, we use the command cf create-domain {organisation name} {domain name}. For me, my organisation is "19FT",… continue reading.