Laravel 5.5 create custom facade example from scratch


In this article, here i will give you step by step guide to create a custom facade in Laravel 5.5 application. You can also learn how to create custom service provider and custom aliases for your class in laravel 5.

Ok So, We may need to create custom facade for creating general helper, some method that always use in whole application like here in example, i created getUserImage() method for getting pull path of user image, Here i will check file is exist or not on following on path then return full path. But it is as an example, So that way you can create your method that you reuse in whole application.

So, Just follow few step to create custom facades in Laravel 5.5 application.

Step 1: Create Your PHP Helper Class

First thing is we need to create your php helper class, Here you will write method of that you re use. So Create "Helpers" folder in app directory and then create MySiteClass.php file as like bellow:

app/Helpers/MySiteClass.php

<?php


namespace App\Helpers;


use File;


class MySiteClass {


public function getUserImage($path)

{

if(!File::exists($path)){

$path = '/image/default.png';

}


return public_path($path);

}

}

Step 2: Bind MySiteClass to ServiceProvider

In this step, we will create our custom service provider and bind MySiteClass to created new custom service provides. So let's run bellow command to create Service Provider.

php artisan make:provider 'MySiteClassServiceProvider'

Next, you will see MySiteClassServiceProvider.php file in "Providers" folder. So let's make bellow code.

app/Providers/MySiteClassServiceProvider.php

<?php


namespace App\Providers;


use Illuminate\Support\ServiceProvider;

use Illuminate\Support\Facades\App;


class MySiteClassServiceProvider extends ServiceProvider

{

/**

* Bootstrap the application services.

*

* @return void

*/

public function boot()

{

//

}


/**

* Register the application services.

*

* @return void

*/

public function register()

{

App::bind('mysiteclass', function()

{

return new \App\Helpers\MySiteClass;

});

}

}

Step 3: Create Custom Facade

In this step, we need to create our custom facade MySiteClass.php, So first create Facades folder and create MySiteClass.php, then put bellow code.

app/Facades/MySiteClass.php

<?php

namespace App\Facades;


use Illuminate\Support\Facades\Facade;


class MySiteClass extends Facade{


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

}

Step 4: Register ServiceProvider and Alias

Now we need to make configuration, So register service provider and alias on app.php file, So let's add like as bellow:

config/app.php

<?php

return [

....

'providers' => [

....

App\Providers\MySiteClassServiceProvider::class

],

'aliases' => [

....

'MySiteClass'=> App\Facades\MySiteClass::class,

]

After this you need to run bellow command:

composer dump-autoload

Step 5: Use Facade

In last step, i will show you how to use we created custom facade, i simple created test route and use our "MySiteClass" facade. So let's see bellow:

routes/web.php

Route::get('/test', function(){

dd(MySiteClass::getUserImage('/upload/users/1.png'));

});

Now you are ready to use Your custom facade.

I hope you found your best...


December 8, 2017 | 9019 Viewer | Category : PHP Laravel Laravel 5 Laravel 5.5
Related Posts