Back in September Taylor Otwell said that Laravel 4.3 would be renamed to Laravel 5 to reflect a directory change and “other interesting initiatives”. Since that announcement the excitement for Laravel 5 has been building and “other interesting initiatives” turned into almost two dozen new features to help developers be more productive.
Some held off upgrading and sat on the sidelines waiting patiently. Others dove in and have been using the develop branch for months, painstakingly looking through commits finding breaking changes.
But all of this is coming to end as Laravel just announced that Laravel 5 will be official next week.
#L5. Next week. — @laravelphp —
Those three words generated a lot of excitement, and for good reason. Months of time and hard work have been put into this version and it’s finally ready for the world.
Laravel 5 is a substantial upgrade with lots of new toys, but at the same time it keeps the parts that made Laravel wildly successful.
In fact there have been so many new features that it’s hard to keep track of just what all is new. In this post I’ll outline the new features and what you have to look forward to next week.
Laravel 5 Directory Structure
The first major change is with the directory structure. The app folder received a facelift and some things got moved outside app. This includes config, database, storage, and resources.
Inside app things are now broken down into further folders including:
Commands
Console
Events
Exceptions
Handlers
Http
Providers
Services
If you are comfortable with previous versions of Laravel this may catch you off guard, but I assure you after about two days of usage it all becomes clear and easy to work with. For more information check out this post by Matt Stauffer.
Blade Changes
Blade didn’t get a whole lot of improvements, but one Blade change is pretty substantial for upgrading.
In Laravel 4 Blade included the following two styles: {{ and {{{.
The double curly bracket was a raw echo and the triple curly bracket escaped.
Now both the double and triple curly brackets escape the variable and
a new {!! $var !!} is for raw.
A common use case for raw is with form helpers:
Contracts
Contracts are a set of interfaces that define the core services provided by the framework. At it’s basics this serves as documentation to the framework’s features. However, they are much more beneficial and you can see the official documentation for more information on these and how they can help.
Commands & Events
Commands and events received a makeover. This feature has been heavily covered both in the docs and from the following resources:
Laravel 5 Events Video
Laravel 5 Commands Video
Commands & Handlers
Events & Handlers
Facades and Helpers
Facades are still in Laravel 5, but new helper functions are starting to replace some of the most common items. For example in Laravel 4 to render a view you would use code like this:
This is now a simple function call:
Some others are:
* abort()
* action()
* app()
* app_path()
* asset()
* Routing – get(), delete(), put()
* back()
* redirect()
* response()
* and many more…
Routes
Routes received two new features. Route caching and middlewares.
Matt did such a great job covering both of these topics I can’t really add anything more. Check out his posts for an in-depth look at both.
Controller Method Injection
Laravel 4 supported controller constructor injection. Now this has been taken a step further and you can type-hint any dependencies in your methods:
or really anything:
Here are some links to go more in depth.
Official Docs
Matt Stauffer
Authentication Scaffolding
A default authentication flow is pre setup and ready for you. Jump starting all this is easier than ever.
This introduces two new traits:
AuthenticatesAndRegistersUsers
ResetsPasswords
The beauty in this is you can use the included or over ride any of the traits methods to customize to your own app.
Socialite
Laravel Socialite is an optional, Laravel 5.0 package that makes dealing with OAuth easier. Something we all hate right? Currently, Socialite supports Facebook, Twitter, Google, and GitHub.
The official docs have more information on implementation and flow.
Flysystem
The next great new feature is the inclusion of Frank de Jonge’s Flysystem. If you are not familiar with this package it is a filesystem abstraction which allows you to easily swap out a local filesystem for a remote one.
The current included adapters are:
Local
Amazon Web Services – S3
Rackspace Cloud Files
Dropbox
Copy
Ftp
Sftp (through phpseclib)
Zip (through ZipArchive)
WebDAV (through SabreDAV)
Azure Blob Storage
NullAdapter
This means you will easily be able to push file uploads to any of these services and use external storage easier than ever before. For most apps I think this alone is going to be a huge time saver.
dotenv – Environmental Detection
Vance Lucas created a PHP package named PHP dotenv that loads environment variables from a .env to getenv(), $_ENV and $_SERVER automagically.
Laravel 5 includes this package to make managing environments much simpler. No more having to edit the bootstrap/start.php and customizing the $app->detectEnvironment() array.
Instead you create your own .env file and can easily customize it to suit your neeeds. Then in any config files you can use something like:
You can also create custom ones for different environments such as with testing. In fact Laracasts has a sample behat integration already utilizing this.
Form Requests
Form validation and requests have been talked about a lot and this is one area I’m sure you’ve heard mentioned.
Basically auth and validation can be turned into FormRequests and injected easily.
Check out the following two resources for more information on this feature:
Form Requests
Laracasts Form Requests and Validation
Laravel Elixir
Elixir is a new wrapper around gulp to make dealing with assets easier. Instead of having to hunt around to build your own css and js build system you can use Elixir and be up and running easily.
Out of the box it handles 90% of everything you need: sass, less, coffeescript, JavaScript, file versioning, and many other features.
Laracasts Elixir Video
Setting up Laravel Elixir with Bootstrap
Laravel Scheduler
Laravel Scheduler is designed to simplify tasks that need to be ran via CRON. All that is required is setting up one cron job that calls artisan schedule:run and have it scheduled every minute.
Once your cron is setup you can schedule any task to run in a concise and friendly manner. It takes the pain out of having to remember cron scheduling and is really simple. These schedules are created inside your “app/Console/Commands” directory.
Here is an example task:
When this feature was announced it received a lot of mixed reactions. I’m in the camp that it’s brilliant. I like that the cron tasks are versioned, easily viewable in my editor, and above all, written in plain English.
New dd()
Everyone’s favorite function dd(), or dump and die, got a big upgrade. Symfony released a VarDumper Component and Laravel now uses this under the hood.
The VarDumper component provides mechanisms for walking through any arbitrary PHP variable. Built on top, it provides a better dump() function that you can use instead of var_dump.
Very useful and love how much more power this gives us. For more information see Laravel dd gets an upgrade.
Eloquent Attribute Casting
This feature comes to us thanks to Dayle Reese. Attribute Casting allows you to convert attributes to another data-type. As an example in your model you add a casts property:
This will automatically cast is_admin to a bool. Options would be stored as json and automatically converted to an array when coming out.
This is brilliant when you are using JavaScript and needing to match them up.
Whoops no more
Laravel 4 featured an error handler named Whoops. This has been removed from Laravel 5 but if you want it back see Bringing Whoops Back to Laravel 5.
Packages and Workbench
The Laravel package system has some substantial changes. The most notable is Workbench is now removed in favor of just using Composer packages directly.
You can find out more about the specific changes around these in the official docs and a from a tutorial by John in ‘t Hout, Loading package views / language files
Psysh
Tinker, the CLI tool to play around with your code, now is using Psysh by Justin Hileman. For those not familiar Psysh is:
A runtime developer console, interactive debugger and REPL —Read-Eval-Print Loop— for PHP
Psysh is super powerful, here is some of the features:
Read documentation in context. doc dd
The list command knows all about your code — and everyone else’s. Easily list and search all variables, constants, classes, interfaces, traits, functions, methods and properties.
Easily show the source code for any userland object, class, interface, trait, constant, method or property.
Catches the last exception and is available via a wtf command.
Show, search, save and replay your shell history.
I’m sure it does much more and will be a great tool in your arsenal.
SuperClosure
SuperClosure, a library for serializing closures and anonymous functions by Jeremy Lindblom, is also a new feature. Under the hood this is being used by the queued closures.
New Generators
Artisan is now even more powerful and can generate a lot of boilerplate for you. Here is a list of some of the ones available now:
make:command — Create a new command class
make:console — Create a new Artisan command
make:controller — Create a new resource controller class
make:event — Create a new event class
make:middleware — Create a new middleware class
make:migration — Create a new migration file
make:model — Create a new Eloquent model class
make:provider — Create a new service provider class
make:request — Create a new form request class
event:generate — Generate the missing events and handlers
Upgrading
Upgrading is not going to be a simple composer update. As this is a major version you should expect to spend a few hours getting everything updated. Two guides have already been written to help you in the process. The official docs and Matt’s Upgrade Guide.
Where to go next?
As with any framework the best place to go to learn more is the official documentation. After that subscribe to the Laravel Newsletter which will keep you up to date on the latest Laravel tips and tutorials, Laravel packages, and everything else related to Laravel.
2015 is going to be exciting year in the Laravel world and I’m looking forward to keeping you up to date!
Laravel 5 is a post from Laravel News.