Skip to main content

Changes to Packages and Configs in Laravel 5

I started a new project late in 2014 which I knew would be fertile ground to keep pace with the what's happening in Laravel 5.  It's been a great learning experience merging in changes from the upstream Laravel project branch as they happened.

The keen observer might have noticed however that Taylor made a commit on December 4th that "broke" support for packages designed to target Laravel 4.

Patience is key, so I was happy to continue waiting for the next great thing.

In Laravel 4, packages allow their configurations to be overridden per environment.  A package can ship a default configuration which can then be copied into the project as a template to be combined with the original's settings at runtime.  Think of it as a really tricked out array_merge_recursive.
By removing ServiceProvider::package, what's being conveyed is that the automatic cascade behaviour is now deprecated.

This might seem distressing, but don't worry.  Any behaviours you've created that might have depended on arrays are still good.  It's just more deliberate - and luckily enough, a great discussion sparked up on twitter yesterday which helped clear a way forward.

(I said it before, but I'll say it again, I really appreciate Taylor's patience as I muddled through understanding the changes.)

I was a bit confused at this point, because I was expecting there to be some kind of magical mechanism between the package and the project still.  Not realizing of course that rather than expecting the two to couple, you simply leverage your project's ServiceProvider to do any customizations.  I had been creating project-level ServiceProviders in my Laravel 4 projects for a while now but more importantly:

This really makes a lot of sense and while I didn't struggle with L4 configs, I'm sure I could see how they came off as a bit arcane to some.  There are a few circumstances where I think people went a bit off the rails with trying to extend or modify the config systems.  Those are their messes to own, but at least now it won't result in them mis-blaming the framework.

Okay, so where does that leave us?  Well, if you're still wondering how you can merge configs and simplify things, there's really just one tweet left in the discussion that helped me understand:

The simplest summary I can offer based on this is that instead of asking Laravel to load and combine configurations, you are now responsible for providing those arrays yourself.  Fear not however, because you can still merge them conveniently using Illuminate\Config\Repository::set.  The reader is encouraged to also look at the utility method array_set and Arr::set.
If you're going to be parsing config for your pacakge, I'd suggest familiarizing yourself with ServiceProvider::register and ServiceProvider::boot.  It's worth noting that boot is optional and supports method-injection from the framework.  This explains why the method isn't present or marked as abstract in the parent.

They're run by the framework in order of register and then boot, so my understanding right now is that you'd do all your config parsing in register and then leverage that configuration in boot or closures.


I'd encourage you not to see these changes as the loss of a feature, but an attempt to help you simplify your projects.  There's been a great push to apply 12factor practices in Laravel 5 - namely configuration and parameterizing your applications. When you decide to dive into using Docker and other hosting setups, you'll be glad these sorts of things happened!

In terms of simplifying resource and configuration file loading - I think Taylor has something in mind.  We simply have to be patient.

Hopefully this post has cleared things up for you.  If I've missed anything or still have it wrong, let me know with a comment!

Popular posts from this blog

Amazon in Winnipeg?

Late last week, Amazon put word out that they're starting to look for a city to locate a second headquarters.  The fuel this announcement provides has the hype train in Winnipeg going so fast, it's missing stops.

I'm curious though.  As you've been digesting this exciting news, who have you spoken or listened to?
Was it this guy and his party's historic theft of public infrastructure (pdf) and incompetence with digital strategy? Or how about this guy, who maintains that Winnipeg doesn't need a proper rapid transit option?  You know, the kind that might actually catch the eye of handsome Amazon who now has Winnipeg trying to be the belle of the ball.

Runner up articles are Winnipeg's inability to join the 21st centry on active transport and the compounding of the aforementioned digital strategy mistakes.
Stop Listening to These Guys They are not technology experts. At worst, they're unabashed self promoters. At best they're conduits for very bad pol…

The Cons, Winnipeg's New Splash Pads

I never had the chance to be ungrateful, the new splash pad at Kildare and Wabasha isn't a splash pad at all.  It was taken away from us and replaced with an "aquatic park", a little bit of wordsmithing designed to gloss over the fact that an open piece of our community has been replaced with yet another closed gate.

As I write this post now, I can hear it already: "Taken away, what?! It's a new water park, you're so..."
Sure, some might reach for that tired recrimination, which is why I started this blog post by dismissing such a premise.  To be fair however, I offer the response: Don't spoil this discussion with nonsense.
You see, I was grateful before the renovations happened.  The communal service on offer was adequate and I never complained about it or saw it as flawed.  Don't believe me? Here, this is a cute google-generated animation of my son enjoying the splash pad in 2014.

Today we took the kids to see if we could spend some time at th…

Winnipeg Devs! Let's get WPL to carry O'Reilly Safari!

Short and sweet. I discovered the other day that the Toronto Public Library actually gives members access to O'Reilly Safari -- I was instantly jealous! You probably already know that this is a pretty good service chock-a-block full of great resources.

The good news is that we have a way to request new content from the Winnipeg Public Library.  This includes digital content, so I've done exactly that!
If you're interested in adding your voice to mine, I've put together a letter that you need to copy/paste and enter into their simple form. Here's a copy that you can read as part of this post:

This is a request for ongoing and full access to the complete O'Reilly Safari catalogue via a Winnipeg Public Library card. I am filing this under "Magazine Purchase" as it's not a single book, but an ongoing service. Following is a list of the most prominent benefits of adding this service to the collection:
O'Reilly produces extremely high quality conten…