How to create custom facade in laravel 5.2?

How to create custom facade in laravel 5.2?

In this post we will give you information about How to create custom facade in laravel 5.2?. Hear we will give you detail about How to create custom facade in laravel 5.2?And how to use it also give you demo for it if it is necessary.

Sometimes we are use repeat code and repeatedly use function at that time we can create custom facade for our project, that way you can create functions and use it.So, How to generate custom facade, I give you the few step to create custom facade:

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

In Following step, first you should create “ItSolution” directory in app folder and create file DemoClass.php(app/ItSolution/DemoClass.php).now copy the following code in your DemoClass.php file.

app/ItSolution/DemoClass.php

namespace AppItSolution;

class DemoClass {

public function productImagePath($image_name)

{

return public_path('images/products/'.$image_name);

}

public function changeDateFormate($date,$date_format){

return CarbonCarbon::createFromFormat('Y-m-d H:i:s', $date)

->format($date_format);

}

}

Step 2:Create ServiceProvider

In this step you should create service provider for bind class, so fire your terminal or cmd this following command:

php artisan make:provider 'DemoClassServiceProvider'

Ok, now you can find new file DemoClassServiceProvider.php in your providers(app/Providers) directory.So, Basically open app/Providers/DemoClassServiceProvider.php and copy the following code:

app/Providers/DemoClassServiceProvider.php

namespace AppProviders;

use IlluminateSupportServiceProvider;

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 AppItSolutionDemoClass;

});

}

}


Step 3:Create Facade Class

In this step create another class app/ItSolution/DemoClassFacade.php, In this class you have to extend “IlluminateSupportFacadesFacade” class, copy following link:

namespace AppItSolution;

use IlluminateSupportFacadesFacade;

class DemoClassFacade extends Facade{

protected static function getFacadeAccessor() { return 'democlass'; }

}

Step 4:Register Service Provider

Simple Register your service provider in Configapp.php like this way :

// In providers array

AppProvidersDemoClassServiceProvider::class,

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

// In aliases array

'DemoClass'=> AppItSolutionDemoClassFacade::class

Step 5:composer dump

This is the last step and you have to do just composer dump-autoload in your terminal:

composer dump-autoload

At Last you can use this way:

Also see:Laravel Create Custom Artisan Command with Example
Route::get('democlass', function(){

$imagepath = DemoClass::productImagePath('image.jpg');

print_r($imagepath);

});

Hope this code and post will helped you for implement How to create custom facade in laravel 5.2?. if you need any help or any feedback give it in comment section or you have good idea about this post you can give it comment section. Your comment will help us for help you more and improve us. we will give you this type of more interesting post in featured also so, For more interesting post and code Keep reading our blogs

For More Info See :: laravel And github

Leave a Comment

Your email address will not be published. Required fields are marked *

2  +  5  =  

We're accepting well-written guest posts and this is a great opportunity to collaborate : Contact US