Thursday, July 14, 2016

How to create custom facade in laravel?

custom facade




Why using custom Facade?

To reduce code repetition, reduce repeated function in your application. So code will be more optimized and organized. So custom Facade will help you to use the function in anywhere in your application. So let's start how you can create a custom facade.

1. First create a file in App directory in your project. Here as an example i have created a file named javed. And then create a class called DemoClass.php inside in your javed folder. So directory would be like: App\javed\DemoClass.php

inside of your DemoClass.php let's write a function which we gonna use in different places. Write the code like following way:

<?php 
namespace App\javed;

class DemoClass {
    public function calculateSum($a,$b)
    {
        return $a+b;
    }
  
 public function calculateSub($a,$b)
 {
  return $a-$b;
 }
}
2. Now we have crate a service provider for binding the class. So let's open cmd in your project and run the following command:

php artisan make:provider 'DemoClassServiceProvider'
it'll create a empty ServiceProvider class inside of your App\Providers directory named DemoClassServiceProvider.php

3. Now in DemoClassServiceProvider.php class rewrite the register() method in following way:

DemoClassServiceProvider.php:
<?php

namespace App\Providers;

use Illuminate\Support\ServiceProvider;
use App;

class DemoClassServiceProvider extends ServiceProvider
{
    /**
     * Bootstrap the application services.
     *
     * @return void
     */
    public function boot()
    {
        //
    }

    /**
     * Register the application services.
     *
     * @return void
     */
    public function register()
    {
         App::bind('democlass', function()
        {
            return new \App\javed\DemoClass;
        });
    }
}

4.  Now we have create a Facade class inside of App\javed directory:

<?php 

namespace App\javed;
use Illuminate\Support\Facades\Facade;

class DemoClassFacade extends Facade
{
    protected static function getFacadeAccessor()
     { 
      return 'democlass';
      }
}

5. Now we have to register our own created ServiceProvide  in Config\app like the following way:

// In Providers Array

App\Providers\DemoClassServiceProvider::class,

// In Aliases Array

 'DemoClass'=> App\javed\DemoClassFacade::class,
6. Now run the following command in cmd:

composer dump-autoload

7. Our task almost done. Now Let's see how we can use the Facade in  in our application:

Route::get('democlass', function(){
    $sum = DemoClass::calculateSum(3,4);
    $sub = DemoClass::calculateSub(6,3):
    print_r($sum); echo '<pre>';
    print_r($sub);
});

Now if you access the route, We'll see the the output in first line as 7 and second line as 3.

So that's it, Now you can use this calculateSum() and calculateSub() in anywhere in your application.


Hope this tutorial is useful for you. Important topics will be covered soon. So stay connected with my blog. Thank you!





































Related Articles

0 comments:

Post a Comment