This is not the only way to implement facades in Laravel 5, but here is how I did it .
We are going to create our own Foo facade, available in the Foobar namespace.
1. Create your own class
First, for this example, I will create a new folder in my project. He will receive his namespace , which will facilitate his search.
In my case, the directory is called Foobar :

Here we will create a new PHP file with our class definition. In my case, I called it Foo.php .
<?php // %LARAVEL_ROOT%/Foobar/Foo.php namespace Foobar; class Foo { public function Bar() { return 'got it!'; } }
2. Create a facade class
In our new new folder, we can add a new PHP file for our facade. I will call it FooFacade.php , and I put it in another namespace called Foobar \ Facades . Keep in mind that the namespace in this case does not reflect the folder structure!
<?php // %LARAVEL_ROO%/Foobar/FooFacade.php namespace Foobar\Facades; use Illuminate\Support\Facades\Facade; class Foo extends Facade { protected static function getFacadeAccessor() { return 'foo'; // Keep this in mind } }
- Remember that you will return to
getFacadeAccessor as you will need it in a moment.
Also note that you are extending the existing Facade class.
3. Create a new provider using php artisan
So now we need a fantastic new provider. Fortunately, we have a great artisan tool. In my case, I will call it FooProvider .
php artisan make:provider FooProvider
Bam! We have a provider. Read more about services here. Now just know that it has two functions ( boot and register ), and we will add some code before register . We will go to bind our new provider of our application:
$this->app->bind('foo', function () { return new Foo;
So this part of bind('foo' will actually match what you put in your code FooFacade.php . Where I said return 'foo'; before, I want this binding to fit this. (If I said return 'wtv'; I would say bind('wtv', here.)
In addition, we need to tell Laravel where to find Foo !
So on top we add a namespace
use \Foobar\Foo;
Now view the whole file:
<?php // %LARAVEL_ROOT%/app/Providers/FooProvider.php namespace App\Providers; use Illuminate\Support\ServiceProvider; use Foobar\Foo; class FooProvider extends ServiceProvider { /** * Bootstrap the application services. * * @return void */ public function boot() { // } /** * Register the application services. * * @return void */ public function register() { $this->app->bind('foo', function () { return new Foo; }); } }
- Make sure you use
Foobar\Foo and not Foobar\Facades\Foo - your IDE may suggest an incorrect termination.
4. Add links to config/app.php
Now we need to tell Laravel that we are interested in using these random files that we just created and can do this in our config/app.php .
Remember that in the aliases where I wrote 'Foo' , you will need to specify the name with which you want to refer to your facade. Therefore, if you want to use MyBigOlFacade::helloWorld() around your application, you should start this line with 'MyBigOlFacade' => MyApp\WhereEverMyFacadesAre\MyBigOlFacade::class
5. Update your composer.json
The last code change you need is to update the composer.json psr-4 spaces. You should add this:
"psr-4": { "Foobar\\" : "Foobar/",
Final move
So, now that everything has changed, the last thing you need to do is update the caches both in the composer and in the artisan. Try the following:
composer dumpautoload php artisan cache:clear
Usage and quick test:
Create a route in app/routes.php :
Route::get('/foobar', ' FooBarController@testFoo ');
Then run
php artisan make:controller FooBarController
And add the code so that it now looks like this:
<?php namespace App\Http\Controllers; use Foobar\Facades\Foo; use App\Http\Requests; class FooBarController extends Controller { public function testFoo() { dd(Foo::Bar()); } }
You should get the following line:

Troubleshooting
- If you are done and say that you cannot find the
Foobar\Facades\Foo class , try running php artisan optimize