Pragmatism in the real world

Routing to a controller with Slim 2

In a couple of projects that I've written using Slim Framework 2, I've found it beneficial to organise my code into controllers with injected dependencies; probably because that's how I'm used to working with ZF2. To make this easier, I've written an extension to the main Slim class and packaged it into rka-slim-controller which will dynamically instantiate controllers for you for each route. Defining routes is exactly the same as normal for a Slim controller,… continue reading.

Sending test emails from PHP

I've written before about redirecting email while developing using a trap mail script on my development system. Other people also like MailCatcher. I've recently switched to using a simple PHP script that creates a elm file on disk that can be opened by Apple's Mail client. This solution is much faster as there is no SMTP processing involved. Adam Royle came up with this solution in his article Setup a testing mail server using PHP… continue reading.

Customising Bootstrap 3

I'm sure everyone already knows this, but it turns out that you can customise Bootstrap 3 without having to understand Less. Part of the reason that I didn't realise this is that I run my web browser windows quite small and regularly don't see the main menu of getbootstrap.com as it's hidden being the "three dashes" button. However, there's an option called Customize on it. This page gives you a massive form where you can… continue reading.

Dependency injection in Slim framework 2

Slim framework comes with a Dependency Injection container called Set. The basics The DIC is accessed via the container property of $app. To set, you use the set() method: $app->container->set('foobar', function() { return new Foo\Bar(); } ); If you need a given resource to be shared, then use the singleton method: $app->container->singleton('foobar', function() { return new Foo\Bar(); } ); And then to retrieve from the container, there are multiple ways to do it: $fooBar =… continue reading.

Setup Doctrine Migrations to update MySQL timestamp on update

One project I'm working on uses MySQL exclusively and is also using Doctrine Migrations. I wanted to set up a column called updated that was automatically set to the timestamp of the last time the row was changed. This is done in SQL like this: CREATE TABLE foo ( id INT AUTO_INCREMENT NOT NULL, bar VARCHAR(100) NOT NULL, updated timestamp DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP, PRIMARY KEY(id) ); It's not quite obvious how to do… continue reading.

Shorter directory text in Bash prompt

Rather helpfully, David Goodwin left a comment about how he shortens the space taken up by the directory section of his terminal's PS1 prompt by using a Bash script to remove the middle portion. This is a really good idea, so I ported it into my PS1 set up which resulted in some rearranging and thought I'd share here as I modified for OS X and I don't want to lose it! The relevant portion… continue reading.

Setting OS X's Terminal Tab to the current directory

I use many tabs in a Terminal window quite frequently, and while the window title will show the current directory name, the tab title doesn't. You can manually change it using shift+cmd+i, but who can be bothered? Automating it so that the tab title always matches the current directory turns out to be really easy and just requires a few lines in ~/.profile. Firstly, we need a function that sets the tab's title to the… continue reading.

Exclude elements from Zend\Form's getData()

If you need to exclude some elements from a Zend\Form's getData(), the easiest way is to use a validation group. For example: class SomeForm extends \Zend\Form\Form implements \Zend\InputFilter\InputFilterProviderInterface { public function init() { $this->add([ 'name' => 'name', 'options' => ['label' => 'Name'], ]); $this->add([ 'name' => 'email', 'options' => ['label' => 'Email'], ]); $this->add([ 'name' => 'submit', 'type' => 'button', 'options' => [ 'label' => 'Filter', ], ]); $this->setValidationGroup(['name', 'email']); } public function getInputFilterSpecification() {… continue reading.