Bridging Laravel 4 packages to Laravel 5

Craig Morris
2 min readJan 18, 2015

If you’re like a lot of other developers out there you’ve most likely jumped onto Laravel 5 despite it not being even in alpha release and you’ve discovered that a lot of your existing packages are now broken.

Update: Due to popular demand I've now extracted this feature into a package which you can drop in using composer. Read me at https://github.com/morrislaptop/LaravelFivePackageBridges

With the main problem being…

Call to undefined method [package]

This was introduced from this commit with the message of “Working on re-doing package handling”. I haven’t heard any updates on how the new package handling will work though :/

In this post I will show you how to bridge the gap between old and new with Plain Old PHP using inheritance and traits. I’ll be using the Laracasts Flash package as an example.

Step 1: Install the package via composer

composer require laracasts/flash

Step 2: Reference a special bridging service provider instead of the one in the package

'providers' => [
'App\Bridges\FlashServiceProvider'
];

Step 3: Create a trait to be used in your bridging service providers. This trait will override the incompatible base class methods with the Laravel 5 way of doing things.

Step 4: Create a service provider that extends the service provider from the package, configure a couple of variables and then bring in the new methods from the trait.

Now with a new file and just a few lines of code you can have your old packages working in Laravel 5 without modifying any vendor code.

I’ve developed festivalplaylists.io on Laravel 5 and will keep this post up to date with any further bridging requirements. Because we are using the power of traits we only have to update the requirements in one spot.

Enjoy using Laravel 5!

Update: Due to popular demand I’ve now extracted this feature into a package which you can drop in using composer. Read me at https://github.com/morrislaptop/LaravelFivePackageBridges

--

--