How do I create a facade class with Laravel?
It's well explained in that post: http://fideloper.com/create-facade-laravel-4
Hope it helps
Step 1
Create a folder called facades
in your app
folder (app/facades
).
Step 2
Add the facade folder to your composer autoload.
"autoload": {
"classmap": [
...
"app/facades"
]
},
Step 3
Create a Facade file in that folder (FooFacade.php
) and add this content:
<?php
use Illuminate\Support\Facades\Facade;
class MyClass extends Facade {
protected static function getFacadeAccessor() { return 'MyClassAlias'; } // most likely you want MyClass here
}
Step 4
Create a model in app/models
(MyClass.php
).
<?php
namespace MyNamespace;
use Eloquent; // if you're extending Eloquent
class MyClass extends Eloquent {
...
}
Step 5
Create a new service provider (you can create a folder in app called serviceproviders
and add it to composer autoload) (app/models/MyClassServiceProvider.php
).
<?php
use Illuminate\Support\ServiceProvider;
class MyClassServiceProvider extends ServiceProvider {
/**
* Register the service provider.
*
* @return void
*/
public function register() {
$this->app->bind('MyClassAlias', function(){
return new MyNamespace\MyClass;
});
}
}
Here you can add new binding if you want another facade (don't forget to create a facade file if so).
Step 6
Add the service provider to the providers
array in config/app.php
.
'providers' => array(
...
'MyServiceProvider'
)
Step 7
Run composer dump
so we can access our new classes.
Step 8
You can now access MyClassAlias::method()
as a facade.