Laravel 5 Released

Today a new version of Laravel has been unveiled to the masses, Laravel 5 is looking like it will be the best version yet and brings with it a whole collection of new features which make developers lives easier.

As well as a great new framework to play with, we also have a shiny new website and a whole updated set of documentation to get stuck into.

Laravel 5 Website

In this article I wanted to touch on just a handful of these new features, I won’t go in depth, but instead take a 1000ft view of what to expect.

Application Structure

When looking at a new Laravel 5 Application the first noticable difference is the new application structure, this new layout is much more durable and much less cumbersome to deal with.

  • Laravel
    • app
      • Commands
      • Console
      • Events
      • Exceptions
      • Handlers
      • Http
        • Controllers
        • Middleware
        • Requests
      • Providers
      • Services
    • bootstrap
    • config
    • database
    • public
    • resources
    • storage
    • tests
    • vendor

The key points to notice here are the “Commands” and “Handlers” directories, these are to facilitate the new built in command pattern architecture and tools that Laravel 5 ships with.

The “Http” directory is an interesting one, here we can store the routes into our application via HTTP, in theory our application logic should not be coupled with the HTTP layer, so having them seperate in this way is a nice reminder of that.

Also within HTTP we have “Controllers” which is probably an obvious move, but we also introduce (or at least expose) the idea of "Middleware" and "Requests", which allow us to keep all of this logic out of our controllers.

In general the new structure is much more managable, we can seperate concerns much more easily, also notice no models directory!

Middleware

“Middleware” can be a scary word, but as always, it really is quite simple. I recently wrote an article about this, so I won’t go into any depth here, but the idea of being able to decorate your application with additional layers is such a useful concept, and with the conveience of artisan commands to create middleware this offers such a productive path.

php artisan make:middleware SomeMiddleware  

With middleware we are able to easily create filters that can run within our http request cycle either before of after each request, good examples of this are authentication.

If you are interested in learning more about middleware, click here

Command Bus

Command based architecture has been a bit of a 'buzzword' in the Laravel community recently, and whilst commands are awesome, we need to be smart about when we should use them and when we shouldn't, throwing commands into a project because they are "Cool" is something that so many developers are guilty of (myself included) but under the right circumstances they are very useful.

The idea behind using commands is that they provide a convenient method of encapsulating tasks your application needs to perform into a simple, easy to understand "command".

As an example, let's say we are writing some logic in our application to allow users to purchase a book, this task is built up from many smaller tasks such as :-

  • taking a payment
  • arrange shipping or ebook delivery
  • e-mail the customer a receipt
  • update back office systems
  • handle any unusual circumstances such as declined payments etc

The worst course of action would be to just drop all this logic inside your controller method, in the past a preference has been to move these tasks into service layers, but we can be even smarter than this.

php artisan make:command PurchaseBook  

This single command will then stub out our command, which we can then use to make our controllers much neater and less concerned with the details of Purchasing a book.

public function purchaseBook($bookId)  
{
    $this->dispatch(
        new PurchaseBook(Auth::user(), Book::findOrFail($bookId))
    );
}

As demonstrated above, our controllers can be super-lightweight and super-clean using commands, I will be writing a full post on commands and the command pattern in the future, it's a big topic and it deserves more attention.

Round Up

These are just a few things that we have to look forward to later today, as time goes on I will post more articles around new features as I use them, so go download the new version and start playing :)