Nested Language Files in Laravel 5

Posted 28th May 2016

In ear­lier ver­sions of Lar­avel, you were able to use dot nota­tion to refer to lan­guage lines in sub-​directories of your trans­la­tions directory.

So in other words, given the fol­low­ing file structure:

resources
  lang
    en
      site.php
      admin
        settings
          site.php

…you could refer to a line named title in resources/lang/en/admin/settings/site.php like this:

admin.settings.site.title

This no longer works in Lar­avel 5. It’s entirely pos­si­ble, how­ever — it’s just not obvi­ous, nor par­tic­u­larly well documented.

All you need to do is use forward-​slashes, for example:

admin/settings/site.title

Pretty Pagination URLs with Laravel 5

Posted 18th April 2016

It’s entirely down to per­sonal pref­er­ence, but a com­mon ques­tion is how to cre­ate “pretty” pag­i­na­tion URLs.

What I mean by that, is instead of some­thing like this:

example.com/blog?page=2

You end up with a URL which looks like this:

example.com/blog/page/2

Here’s how to do just that in Lar­avel 5, using this pack­age.

For the pur­poses of this post I’m assum­ing you already have a con­troller method which uses pag­i­na­tion; if you don’t then just cre­ate it as nor­mal (the doc­u­men­ta­tion is here if you need it), then come back.

Installing the Package

Building Applications with Backbone and Laravel: JST

Posted 13th April 2014

You might well be used to devel­op­ing Back­bone views like this:

MyApp.Views.SomeView = Backbone.View.extend({    
  template: _.template('<ul><% _.each(items, function(item){ %><li><%= item %></li><% }); %></ul>'),    
  el: '#some-element',

This is all fine and well for sim­ple struc­tures, but can quickly get unwieldly if you’re build­ing markup of any sig­nif­i­cant size or com­plex­ity. It can also lead to all sorts of issues around readability.

One alter­na­tive is to put more com­plex tem­plates into sep­a­rate HTML files:

// file: folder/template.html

<ul>
 <% _.each(items, function(item){ %>
 <li> <%= item %> </li>
 <% }); %>
</ul>

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.

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:

Using Laravel's Pagination Class with JQuery Masonry and Infinite Scroll

Posted 25th July 2012

Just a quick one, but it’s some­thing that I got stuck with for a while.

If you try and use Laravel’s pag­i­na­tion class with JQuery Masonry along with Infi­nite Scroll, you may find a prob­lem. When you scroll down beyond what should be the last page, you’ll prob­a­bly find that you keep get­ting the last page over and and over again, caus­ing gen­uinely infi­nite scrolling. The rea­son for this took me some uncovering.

When Inifi­nite Scroll requests an addi­tional page, it incre­ments the page num­ber, but gets the same con­tent back — how­ever, there is no rea­son why it should know that that’s the case. It’s because it keeps get­ting new con­tent back that it keeps try­ing, but the issue is actu­ally in the Lar­avel Paginator.

The Laravel PHP Framework: A Walkthrough

Posted 5th August 2011

Lar­avel is the new kid on the block of PHP frame­works, and whilst in its early stages of evo­lu­tion (the author wrote it, he says, in the early part of 2011) it does promise — and deliv­ers, from what I can tell — an expres­sive syn­tax and an ele­gant (indeed, you might say elo­quent) ORM. Lar­avel is a fully Object Ori­ented MVC frame­work which makes exten­sive use of fea­tures new or recent to PHP — you can for­get about run­ning it on PHP4. With PHP5.3 com­pli­ant name­spacing mech­a­nisms as opposed to, for exam­ple, Zend Framework’s pseudo-​namespacing, it’s cer­tainly what you might call a mod­ern framework.

Routes

In Lar­avel you define routes — which, stay­ing true to the REST­ful par­a­digm, are defined not just as URI’s, but accord­ing to the HTTP oper­a­tion they are to react to. Thus, instead of defin­ing a route such as /about-us you would define a func­tion to respond to a GET request on that URI, which is done using a clo­sure which looks reas­sur­ingly like JavaScript: