How to create custom facade in laravel 5.2

How to create custom facade in laravel 5.2

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
Step 1 - Create PHP Class File, App\Helpers\GeneralClass.php

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 using email id.

  1. <?php
  2. namespace App\Helpers;
  3. class GeneralClass {
  4. // function to get gravatar photo/image from using email id.
  5. public function getGravatarURL($email, $s = 80, $d = 'mm', $r = 'g', $img = false, $atts = array())
  6. {
  7. $url = '';
  8. $url .= md5(strtolower(trim($email)));
  9. $url .= "?s=$s&d=$d&r=$r";
  10. if ($img)
  11. {
  12. $url = '<img src="' . $url . '"';
  13. foreach ($atts as $key => $val)
  14. $url .= ' ' . $key . '="' . $val . '"';
  15. $url .= ' />';
  16. }
  17. return $url;
  18. }
  19. }
Step 2:Bind Class to ServiceProvider

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.

  1. <?php
  2. namespace App\Providers;
  3. use Illuminate\Support\Facades\App;
  4. use Illuminate\Support\ServiceProvider;
  5. class GeneralClassServiceProvider extends ServiceProvider
  6. {
  7. /**
  8. * Bootstrap the application services.
  9. *
  10. * @return void
  11. */
  12. public function boot()
  13. {
  14. //
  15. }
  16. /**
  17. * Register the application services.
  18. *
  19. * @return void
  20. */
  21. public function register()
  22. {
  23. App::bind('generalclass', function()
  24. {
  25. return new \App\Helpers\GeneralClass;
  26. });
  27. }
  28. }
Step 3: Register this ServiceProvider to providers array

In this step we will register the newly generated service provider to provider array in following path Config\app.php.

Step 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.

  1. <?php
  2. namespace App\Facades;
  3. use Illuminate\Support\Facades\Facade;
  4. class GeneralClass extends Facade{
  5. protected static function getFacadeAccessor() { return 'generalclass'; }
  6. }
Step 5: Register this ServiceProvider to providers array

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.

  1. Route::get('/', function(){
  2. $img='<img src="'.GeneralClass::getGravatarURL('').'">';
  3. echo $img;
  4. });

Phone: (+91) 8800417876
Noida, 201301
okey metin2 pvp serverler metin2 pvp
ataşehir escort maltepe escort bostancı escort kadıköy escort maltepe escort ataşehir escort tuzla escort pendik escort ümraniye escort kartal escort
pendik escort
izmir escort izmir escort izmir escort izmir escort denizli escort antalya escort antalya escort antalya escort izmir escort izmir escort izmir escort izmir escort izmir escort
hatay escort nevsehir escort nigde escort kirikkale escort batman escort osmaniye escort kirklareli escort tokat escort rize escort adiyaman escort gumushane escort batman escort
cialis satış cialis fiyat cialis 20 mg cialis 100 mg cialis 5 mg fiyat

viagra fiyatı

viagra 100 mg fiyat

viagra sipariş

orjinal viagra

viagra satın al

izmir escort
ataşehir escort
kocaeli escort izmit escort bayan gebze escort izmit sınırsız escort escort bursa tuzla escort aydınlı escort orhanlı escort bursa escort escort bayan istanbul escort istanbul sınırsız escort istanbul bayan escort izmit escort izmit escort kocaeli escort sisli escort mecidiyekoy escort bursa escort bayan bursa görükle escort maltepe escort goztepe escort yakacik-yeni-escort bursa sınırsız escort sisli escort bayan taksim escort bursa escort porno izle sikiş izle atasehir escort antalya escort
ankara escort marmaris escort bursa escort mersin escort antalya escort izmir escort samsun escort bodrum escort çeşme escort kuşadası escort adana escort eskişehir escort gaziantep escort esenyurt escort pendik escort tuzla escort fatih escort ümraniye escort mecidiyeköy escort kartal escort bahçelievler escort bahçeşehir escort ataşehir escort bakırköy escort nişantaşı escort avcılar escort büyükçekmece escort kurtkoy escort konya escort beşiktaş escort taksim escort beylikdüzü escort malatya escort sakarya escort şişli escort maltepe escort kadıköy escort ataköy escort bostancı escort şirinevler escort bağcılar escort üsküdar escort etiler escort başaksehir escort ortaköy escort kağıthane escort sultangazi escort halkalı escort çekmeköy escort şaşkınbakkal escort mamak escort sincan escort keçiören escort çankaya escort beypazarı escort akyurt escort pursaklar escort kahramankazan escort göztepe escort alsancak escort bornova escort buca escort karabaglar escort karşıyaka escort konak escort narlıdere escort izmit escort başiskele escort kandira escort körfez escort karamürsel escort dilovası escort gölcük escort gebze escort kocaeli escort kayseri escort elazığ escort denizli escort diyarbakir escort adana escort ankara escort izmir escort antalya escort gaziantep escort kayseri escort konya escort mersin escort denizli escort kocaeli escort diyarbakır escort
bedava bahis