How to Set Up Facades in Laravel 5.2

Reference from: http://stackoverflow.com/questions/35639737/how-to-set-up-facades-in-laravel-5-2-outside-of-app

This may not be the only way to implement facades in Laravel 5, but here is how I did it.

We’re going to create a custom Foo facade available in the Foobar namespace.

1. Create a custom class

First, for this example, I will be creating a new folder in my project. It will get its own namespace that will make it easier to find.

In my case the directory is called Foobar:

enter image description here

In here, we’ll create a new PHP file with our class definition. In my case, I called it Foo.php.

2. Create a facade class

In our fancy new folder, we can add a new PHP file for our facade. I’m going to call it FooFacade.php, and I’m putting it in a different namespace called Foobar\Facades. Keep in mind that the namespace in this case does not reflect the folder structure!

  • Bear in mind what you return in getFacadeAccessor as you will need that in a moment.

Also note that you are extending the existing Facade class here.

3. Create a new provider using php artisan

So now we need ourselves a fancy new provider. Thankfully we have the awesome artisan tool. In my case, I’m gonna call it FooProvider.

Bam! We’ve got a provider. Read more about service providers here. For now just know that it has two functions (boot and register) and we will add some code to register. We’re going to bindour new provider our app:

So this bind('foo' portion is actually going to match up with what you put in your FooFacade.phpcode. Where I said return 'foo'; before, I want this bind to match that. (If I’d have said return 'wtv'; I’d say bind('wtv', here.)

Furthermore, we need to tell Laravel where to find Foo!

So at the top we add the namespace

Check out the whole file now:

  • Make sure you use Foobar\Foo and not Foobar\Facades\Foo – your IDE might suggest the wrong completion.

4. Add our references to config/app.php

Now we have to tell Laravel we’re interested in using these random files we just created, and we can do that in our config/app.php file.

  1. Add your provider class reference to 'providers': App\Providers\FooProvider::class
  2. Add your facade class reference to 'aliases': 'Foo' => Foobar\Facades\Foo::class

Remember, in aliases, where I wrote 'Foo', you will want to put the name you want to reference your facade with there. So if you want to use MyBigOlFacade::helloWorld() around your app, you’d start that line with 'MyBigOlFacade' => MyApp\WhereEverMyFacadesAre\MyBigOlFacade::class

5. Update your composer.json

The last code change you should need is to update your composer.json‘s psr-4 spaces. You will have to add this:

Final move

Okay so now that you have all that changed, the last thing you need is to refresh the caches in both composer and artisan. Try this:

Usage & A Quick Test:

Create a route in app/routes.php:

Then run

And add some code so it now looks like this:

You should end up with the following string:

enter image description here


Troubleshooting

  • If you end up with and error saying it cannot find the class Foobar\Facades\Foo, try running php artisan optimize

273 total views, 1 views today

Trả lời