How to create custom facade in laravel?

Share Me
  •  
  •  
  •  
  •  
  •  
  •  
  •  
  •  
  •  
  •  
  •  

Today now in this blog i will show you how to create facade in laravel. Sometimes we need to use repeat code and repeatedly use same function at that time we can create a custom facade for our project, so that way we can create a functions and we can use it. So here we know about, How to generate custom facade, here I give you the few step to create a new custom facade:

Step 1:Create class file app/CodingPoint/DemoClass.php

At first In Following step, first we should create a “CodingPoint” directory in app folder and then create a file DemoClass.php(app/CodingPoint/DemoClass.php). So now we can copy the following code in our DemoClass.php file.

app/CodingPoint/DemoClass.php

namespace App\CodingPoint;
class DemoClass {
    public function productImagePath($image_name)
    {
        return public_path('images/products/'.$image_name);
    }
    public function changeDateFormate($date,$date_format){
		return \Carbon\Carbon::createFromFormat('Y-m-d H:i:s', $date)
					->format($date_format);
	}
}
Step 2:Create ServiceProvider

So now in this step we should create a service provider for bind class, so fire your terminal or cmd to this following command:

php artisan make:provider 'DemoClassServiceProvider'

So now we can find a new file DemoClassServiceProvider.php in our providers(app/Providers) directory. So, it is basically open app/Providers/DemoClassServiceProvider.php and it will copy the following code:

app/Providers/DemoClassServiceProvider.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\CodingsPoint\DemoClass;
        });
    }
}
Step 3:Create Facade Class

Now in this step we need to create another class app/CodingsPoint/DemoClassFacade.php, So in this class we need to extend “Illuminate\Support\Facades\Facade” class, copy following link:

namespace App\CodinsPoint;
use Illuminate\Support\Facades\Facade;
class DemoClassFacade extends Facade{
    protected static function getFacadeAccessor() { return 'democlass'; }
}
Step 4:Register Service Provider

So Simple Register our service provider in Config\app.php like this way :

// In providers arrayApp\Providers\DemoClassServiceProvider::class,

And also add aliase for facade in this file like this way :

// In aliases array'DemoClass'=> App\CodingsPoint\DemoClassFacade::class
Step 5:composer dump

So now we are in last step. you need to do just composer dump-autoload in your terminal:

composer dump-autoload

At Last you can use this way:

Route::get('democlass', function(){
    $imagepath = DemoClass::productImagePath('image.jpg');

    print_r($imagepath);

});

Read Also : How to push item to 0 index or first of $scope object in AngularJS?

Thanks for read. I hope it help you. For more you can follow us on facebook.

About Shahriar Sagor

My name is Shahriar sagor. I'm a developer. I live in Bangladesh and I love to write tutorials and tips that will help to other Developer's. I am a big fan of PHP, Javascript, JQuery, Laravel, Codeigniter, VueJS, AngularJS and Bootstrap from the early stage.

View all posts by Shahriar Sagor →