You may have heard of the new trendy microframework called Lumen. It is a lighter version of Laravel with a few key changes.

One thing that excited me about Lumen is that by default, "facades" are not enabled. What this means, is you can't write DB::select() (which is a quick shortcut to resolve the depedency and act on it).

Sidenote: Laravel calls this concept "Facades", but it is more accurately the Proxy Pattern.

Why is it bad to use facades?

There is lots to love about laravel, especially its ease-of-use, but like any framework, it's not without its flaws.

Facades are very convenient, but they tie you to Laravel/Lumen (see Coupling). Your code is all of the sudden littered with static calls which can make testing harder and reduce interoperability. David Adams wrote a good article covering how and why not to use facades.

Working without facades

The documentation for using Lumen without facades is lacking. I've started to contribute to the docs to help ease that pain.

To resolve a dependency out of the container, you can use the app() function. So DB::select($sql) becomes $db = app('db'); and $db->select($sql);. Below is an example of resolving the database dependency and using it.

The important part about line 17 is that it is the only line of code that connects Laravel/Lumen's container with the dependency. That means if we put this code into another framework we only need to modify that line (more or less).

Using Dependency Injection

Using dependency injection is another similar solution to not using facades. Note that for the database manager to be injected you need to make a couple of changes to app.php

  1. 1. Uncomment $app->withEloquent(); or add $app->make('db');
  2. 2. Add $app->alias('db', 'Illuminate\Database\DatabaseManager');

After making those changes, that same example could look like this: