Pragmatism in the real world

Overriding Slim 3's error handling

Slim 3 registers two error handers: errorHandler to trap PHP Exceptions phpErrorHandler to trap PHP 7 Errors This means that if you want to override the default error handlers, you need to override both error handlers with your own code. Each error handler is a callable. The signatures are: errorHandler: function ($request, $response, $exception) phpErrorHandler: function ($request, $response, $error) To override a error handler, simply register a new callable with the Container: $container = $app->getContainer();… continue reading.

Using Eloquent in Slim Framework

Eloquent is one of the easier ORMs to get started with, so let's look at how to use and test it within a Slim Framework application. Set up You can add Eloquent to your Slim project via composer: composer require illuminate/database You now need to bootstrap it. To do this we need some settings, so lets do that in our standard Slim configuration: $config = [ 'settings' => [ 'db' => [ // Eloquent configuration… continue reading.

Free the Geek appearance

I was delighted to spend an hour chatting with Matt Setter on episode 14 of Free the Geek. In this podcast, we talked about Slim 3 and development in general. Matthew gives me a great introduction (thanks!) and then we delve into the chat. We talk about the importance of semantic versioning as I think this is key to stability in a project. It's not very glamorous to work on code where you have to… continue reading.

A few composer tips

I recently learned about a couple of features of composer that I thought I'd write down here so that I don't forget them! I also had to deal with a conflict in composer.lock recently, so I've noted down how I solved that too. List installed versions To list the current versions of all installed dependencies: composer show -i The output looks something like: container-interop/container-interop 1.1.0 Promoting the interoperability of container objects… monolog/monolog 1.17.2 Sends your… continue reading.

Configuration in Slim Framework

Configuration in Slim Framework is nice and simple: the App's constructor takes a configuration array for the DI container; $config = []; $app = new Slim\App($config); Setting up The settings sub-array is used to hold the settings of your application: $config = [ 'settings' => [ 'displayErrorDetails' => true, 'logger' => [ 'name' => 'slim-app', 'level' => Monolog\Logger::DEBUG, 'path' => __DIR__ . '/../logs/app.log', ], ] ]; $app = new Slim\App($config); Slim comes with a number… continue reading.

Testing Slim Framework actions

To test a Slim Framework action, you need a request and a response object and mock whatever is in the action. This is one way to do this. Consider this simple echo action that returns the query parameters to you as a JSON encoded string: $ curl "http://localhost:8888/echo?foo=bar&this=that" {"foo":"bar","this":"that"} This is one of those useful API endpoints that your users can use to check that everything is working as expected. The action under test The… continue reading.

Determining the image type of a file

One thing I learnt recently which I probably should have known already is that getimagesize() returns more than just the width and height of the image. I've always used it like this: list($width, $height) = getimagesize($filename); However, getimagesize() also returns up to 5 more pieces of information. Interestingly, the data array is a mix of indexed elements and named elements For example, for a file I uploaded while testing a PR, the output of print_r(getimagesize($filename))… continue reading.

Improved error handling in Slim 3.2.0

We released Slim 3.2.0 yesterday which includes a number of minor bug fixes since 3.1.0 and also a few nice improvements around the way we handle errors. Writing to the error log Slim has a simple exception handler implemented that displays an error page that looks like this: It's not very informative, is it? That's intentional as we don't want to leak information on a live website. To display the error information you need to… continue reading.

Team culture and diversity

Last Friday, I attended a course on managing people led by Meri Williams and learnt a lot. I highly recommend booking her next course if you can. During the Q&A session, there was a question about hiring for diversity and Meri had some very interesting thoughts. I won't try to reproduce them all here as I'll be doing her a disservice. One comment that resonated was that ideally you want your team members to be… continue reading.

Use vim's :make to preview Markdown

As it becomes more painful to use a pointing device for long periods of time, I find myself using vim more and so I'm paying more attention to customisation so that the things I'm used to from Sublime Text are available to me. One thing I'm used to is that when I run the build command on a Markdown file, I expect Marked for Mac to open and render the file that I'm writing. Vim… continue reading.