How to create custom facade in laravel 5.2
Benefits of creating custom facade in Laravel 5.2 is it that you don't have to write same function again and again, to avoid repetition you can create a helper class or can say facade class.
Facades basically provide a static interface to a class that give access to an object from the service container.
You will have to simply define single method getFacadeAccessor
for a facade class.
Here you will know how to create custom facade in Laravel.
You will only need 3 things to create facade class for your application :
- Bind class to Service Provider
- Create facade class
- Configuration facade alias
In this step, you will have to create a "Helpers" directory within app folder and create PHP class "GeneralClass.php" file within Helpers directory.
We will create this helper class to get gravatar photo/image from gravatar.com using email id.
app/Helpers/GeneralClass.php
- <?php
- namespace App\Helpers;
- class GeneralClass {
- // function to get gravatar photo/image from gravatar.com using email id.
- public function getGravatarURL($email, $s = 80, $d = 'mm', $r = 'g', $img = false, $atts = array())
- {
- $url = 'http://www.gravatar.com/avatar/';
- $url .= md5(strtolower(trim($email)));
- $url .= "?s=$s&d=$d&r=$r";
- if ($img)
- {
- $url = '<img src="' . $url . '"';
- foreach ($atts as $key => $val)
- $url .= ' ' . $key . '="' . $val . '"';
- $url .= ' />';
- }
- return $url;
- }
- }
In this step we will create new service provider to bind class.
Execute this command in your terminal :
php artisan make:provider 'GeneralClassServiceProvider'
After running this command you will get message of 'Provider created successfully' and new provider file is generated within Providers directory.
app/Providers/GeneralClassServiceProvider.php
- <?php
- namespace App\Providers;
- use Illuminate\Support\Facades\App;
- use Illuminate\Support\ServiceProvider;
- class GeneralClassServiceProvider extends ServiceProvider
- {
- /**
- * Bootstrap the application services.
- *
- * @return void
- */
- public function boot()
- {
- //
- }
- /**
- * Register the application services.
- *
- * @return void
- */
- public function register()
- {
- App::bind('generalclass', function()
- {
- return new \App\Helpers\GeneralClass;
- });
- }
- }
In this step we will register the newly generated service provider to provider array in following path Config\app.php.
App\Providers\GeneralClassServiceProvider::classStep 4: Create facade Class
In this step we will create a directory first where all custom facade class will be created and all custom facade must extends to Illuminate\Support\Facades\Facade
.
So create Facades directory within app directory and then create your facade class within this directory.
app/Facades/GeneralClass.php
- <?php
- namespace App\Facades;
- use Illuminate\Support\Facades\Facade;
- class GeneralClass extends Facade{
- protected static function getFacadeAccessor() { return 'generalclass'; }
- }
In this step we will add aliases for this facade class in aliases array in following path Config\app.php
'GeneralClass'=> App\Facades\GeneralClass::class
Now finally you will run composer dump-autoload
command to load custom folder and files.
Now you have created a custom facade for your application, you can check, It works or not by following code which i write in routes.php.
- Route::get('/', function(){
- $img='<img src="'.GeneralClass::getGravatarURL('ajay.agrahari09@gmail.com').'">';
- echo $img;
- });