Legal Pages in Laravel

Posted 13th March 2014

Most sites have pages for Terms and Con­di­tions, a Pri­vacy pol­icy and /​or a page about cook­ies. Pre­sum­ably they are usu­ally tucked away in a CMS some­where, or as sta­tic HTML — no doubt copied & pasted ver­ba­tim from the Inter­net. Whether the CMS is the nat­ural place for them remains to be seen, and cer­tainly if a site doesn’t have con­tent as such then sup­port­ing those sorts of pages intro­duces new prob­lems — is the con­tent sim­ply dropped into a view somewhere?

To help solve this prob­lem — in Lar­avel 4, specif­i­cally — I’ve cre­ated a sim­ple pack­age called Legal­pages. The idea behind it is sim­ple — you install it, enter a few pieces of infor­ma­tion about your busi­ness or organ­i­sa­tion in a con­fig­u­ra­tion file — and done, your site now has Terms and Con­di­tions, Pri­vacy and cookie pages. I’ve used some boil­er­plate doc­u­ments, which get tai­lored accord­ing to the infor­ma­tion you pro­vide, but you can cus­tomise them if you wish — they wind up in your app/storage direc­tory in Mark­down for­mat for easy editing.

Working with UK Postcodes in PHP

Posted 6th March 2014

I’ve cre­ated a sim­ple pack­age for work­ing with UK Post­codes. It con­tains two prin­ci­ple components:

  1. A sim­ple class to encap­su­late UK Postcodes
  2. A wrap­per to this web ser­vice

The post­code class can be used for:

  • val­i­dat­ing postcodes
  • for­mat­ting them, e.g. “sw1a2aa” -> “SW1A 2AA
  • get­ting the out­code (the first part of a post­code; e.g. M1, SW1A, GL9)

The meat and bones of the pack­age, though, is the web ser­vice. Most notably, it allows you to take a post­code, and find out its lat­i­tude and lon­gi­tude. Here’s an exam­ple of how it’s used:

// Create the client
$client = new Lukaswhite\UkPostcodes\UkPostcodesClient();       

// Call the web service
$postcode = $client->postcode('sw1a2aa');

print get_class($postcode);
// Lukaswhite\UkPostcodes\UkPostcode

print $postcode->formatted();
// SW1A 2AA

print get_class($postcode->getCoordinate());
// League\Geotools\Coordinate\Coordinate

print $postcode->getCoordinate()->getLatitude();
// 51.503539898876

print $postcode->getCoordinate()->getLongitude();
// -0.12768084037293

print get_class($postcode->getCoordinate()->getEllipsoid());
// League\Geotools\Coordinate\Ellipsoid 

print $postcode->council->title;
// City of Westminster

print $postcode->council->code;
// E09000033

print $postcode->council->uri;

print $postcode->ward->title;
// St. James's

print $postcode->constituency->title;
// Cities of London and Westminster

Incorporating Gravatar into the Signin (or Registration) Process

Posted 26th February 2014

I signed up for an online ser­vice the other day (Com​mando​.io), and they had a really nice lit­tle inter­ac­tion dur­ing the signup and signin processes. As soon as you enter your email address, it auto­mat­i­cally dis­plays your Gra­vatar. Nice lit­tle touch.

So, I thought I’d write a quick tuto­r­ial on how to repli­cate the effect.

There is a demo here.

Get­ting Started

For sim­plic­ity, I based the form on one of the exam­ple tem­plates for the Boot­strap frame­work.

It’s pretty straight­for­ward to down­load the Boot­strap frame­work using Bower:

Running Laravel Artisan Commands from your Admin Dashboard / GUI

Posted 22nd January 2014

Some­times you may wish to run Arti­san com­mands from your admin dash­board /​appli­ca­tion, with­out using the com­mand line. Per­haps you don’t have access to the com­mand line (in which case per­haps it’s time to switch host­ing!), or more likely per­haps you want to man­u­ally run tasks right from your appli­ca­tion. Here’s a quick guide to how you might set that up.

Run­ning a command

To run a com­mand pro­gram­mat­i­cally, you sim­ply do this:

Artisan::call('my-command', array());

The first argu­ment is the name of the com­mand, the sec­ond your options, which I’m going to ignore for brevity.

It’s prob­a­bly more use­ful if you can get a hold of the command’s out­put; you can do this by pass­ing a third argu­ment, which should be a class that imple­ments Symfony\Component\Console\Output\OutputInterface. Per­haps the most use­ful of these classes is StreamOutput. So, for exam­ple, you can write the out­put to a file:

Laravel Pagination with Foundation

Posted 19th December 2013

Lar­avel 4’s pag­i­na­tion func­tion­al­ity sup­ports Twit­ter Boot­strap out-​of-​the-​box. How­ever, if you pre­fer to use Zurb’s Foun­da­tion, here is the code you’ll need. Just drop the pre­sen­ter class some­where where your autoloader will pick it up, pick one (or both) of the tem­plate files, and con­fig­ure your app/config/view.php file to point to the appro­pri­ate tem­plate file.

First, the pre­sen­ter class:

The sim­ple tem­plate file:

The slider tem­plate file:

Recent Sitepoint Articles

Posted 6th October 2013

I haven’t writ­ten any­thing here for quite a while, but I have been writ­ing. I’ve had quite a num­ber of arti­cles pub­lished on Site­point (and before the re-​brand, the PHP­Mas­ter site).

Here’s a list of my lat­est articles.

Cre­at­ing a Subscription-​Based Web­site with Lar­avel and Recurly, Part 1 and Part 2

- Site­point, Sep­tem­ber 2013

Build­ing a subscription-​based web­site or appli­ca­tion with Lar­avel, using Recurly to process recur­ring pay­ments. In this first part I look at set­ting up the appli­ca­tion, con­fig­ur­ing users and roles, and build­ing an authen­ti­ca­tion system.

A Bash Script for New projects

Posted 22nd August 2013

When­ever I start a new web project, the ini­tial steps are pretty much the same:

  • Cre­ate a new folder, in my case in /var/www
  • Change the own­er­ship of the new folder
  • Come up with a new host­name, e.g. myproject.local
  • Cre­ate an entry in etc/hosts
  • Cre­ate an Apache vir­tual host for it
  • Cre­ate a new database
  • Cre­ate a new Git repository
  • Intialise the Git repository
  • Cre­ate a new project in Sublime

Not par­tic­u­larly hard, but pretty repet­i­tive. And if something’s repet­i­tive, chances are it can be scripted!

So here’s what I came up with:

Website Re-Development

Posted 11th August 2013

Until very recently, this web­site was pow­ered by Dru­pal. Now although Drupal’s great in a whole raft of sit­u­a­tions, there are times when either it’s overkill for some­thing small, or just doesn’t fit into one’s work­flow. I came to realise that both of these applied to this site, so when it came to re-​developing, I decided to aban­don Dru­pal for the site altogether.

In the end, I decided to rebuild the site using Jekyll, along with a few other tools along the way.

This post explains the moti­va­tions behind the deci­sion, what I did next and some of the chal­lenges I faced along the way.

The Need for Re-​development

There were sev­eral aspects that made re-​developing my site some­thing of a priority.

First, it wasn’t mobile-​friendly. I may have been able to get away with that some years ago when I first drafted the site, but now that’s just not accept­able. After weigh­ing the pros and cons of a respon­sive vs a sep­a­rate mobile site, I decided on the for­mer — and the only way to do that would be to start the front end build again from scratch.

Gift Certificates in Drupal Commerce

Posted 4th May 2013

There isn’t really an out-​of-​the-​box solu­tion for gift cer­tifi­cates in Dru­pal Com­merce; there’s no mod­ule (at time of writ­ing) you can sim­ply down­load, enable and for­get about. Being Dru­pal, there are many ways to skin a cat, so to speak, so in this post I’m going to out­line how I got them work­ing on a site I built.

Of course, the way your gift cer­tifi­cates work may well dif­fer from site-​to-​site; I had a way I wanted it to work, but your require­ments might be sub­tly dif­fer­ent, but hope­fully what­ever your require­ments, some of what fol­lows is of some use.

To out­line, here’s what I wanted to achieve:

  1. Gift cer­tifi­cates avail­able as prod­ucts, which a cus­tomer can purchase
  2. A range of val­ues (£5, £10, £20 etc) available
  3. A prod­uct dis­play page that dif­fered from “con­ven­tional” prod­ucts — there are less fields required, so the lay­out was likely to be different
  4. I wanted a cus­tomer to be able to spec­ify a recip­i­ent for the gift certificate
  5. The cus­tomer needed to sup­ply an email address for the recip­i­ent, so we can send it to them via email
  6. I wanted the cus­tomer to be able to spec­ify the recipient’s name, to per­son­alise the email
  7. The cus­tomer ought to be able to enter their name as they wished it to appear on the email; the billing name on the order might well be too formal
  8. There ought to be an optional text field to add a per­sonal message
  9. The gift cer­tifi­cate needed to be sent out via email when the order was com­pleted, and paid for

Writing a Book in Markdown

Posted 28th February 2013

I’m writ­ing my first book at the moment. Lately I’ve been doing all my writ­ing in Mark­down, but ulti­mately i’ll need an alter­na­tive for­mat. For word pro­cess­ing, such as it is, I tend to use Open Office — so really I wanted to be able to write in Mark­down, but con­vert to Open Office (or indeed, any of a vari­ety of for­mats). For one thing, it’s nice to get an idea of the num­ber of pages, which my Markown edi­tor just can’t do. Also, I pre­fer to work with sep­a­rate files for each chap­ter — but I’m not entirely sure what order the chap­ters will end up in. So, what I really wanted was a quick way to do two things — con­cate­nate all my chap­ters, and then con­vert the lot to Open Office format.

My solu­tion to this is in two parts.

First, a shell script to con­cate­nate the files. It’s impor­tant it’s fairly easy to rearrange chap­ters, so the for­mat of this was crucial.