Link Search Menu Expand Document

Facades

Resources of this tutorial

  • https://www.sitepoint.com/how-laravel-facades-work-and-how-to-use-them-elsewhere/
  • https://www.youtube.com/watch?v=zR6JnwH7MSQ&t=13s++++++
    • In this tutorial it uses the splat operator …
    • https://stackoverflow.com/questions/41124015/meaning-of-three-dot-in-php
  • https://stackoverflow.com/questions/40784181/how-to-access-a-packages-methods
  • https://stackoverflow.com/questions/25289124/package-facade-not-found

What is a Facade in Laravel?

As you probably know, every service inside the container has a unique name.

In a Laravel application, to access a service directly from the container, we can use the App::make() method or the app() helper function.

<?php

App::make('some_service')->methodName();

Laravel uses facade classes to make services available to the developer in a more readable way. By using a facade class, we would only need to write the following code to do the same thing:

// ...
someService::methodName();
// ...

In Laravel, all services have a facade class. These facade classes extend the base Facade class which is part of the Illuminate/Support package. The only thing that they need to implement is the getFacadeAccessor() method, which returns the service name inside the container.

In the above syntax:

  • someService refers to the facade class
  • The methodName is in fact a method of the original service in the container

Practical example

From:

  • https://www.youtube.com/watch?v=zR6JnwH7MSQ&t=13s++++++
    • In this tutorial it uses the splat operator …
    • https://stackoverflow.com/questions/41124015/meaning-of-three-dot-in-php
  1. Create a service (17:18)
    • This service sends an email and dump a message.

app/Services/PostcardSendingService.php

 <?php
 
 namespace App/Services;
 
 class PostcardSendingService{
  private $country;
  private $width;
  private $height;
 
  public static function __construct($country, $width, $height){
    $this->country = $country;
    $this->width = $width;
    $this->height = $height;
  }

  public function hello(){
    Mail::raw($message, function($message) use($email){
      $message->to($email);
    });

    dump("A postcard was sent with the message: " . $message );
  }
}
  1. Register in the App service provider a singleton.
    • This singletone has going to be referenced as Postcard.
    • Here what we are outputting is how to generate the PostcardSendingService.

app/Providers/AppServiceProvider.php

public function boot(){
  $this->app->singleton('Postcard', function($app){
    return new PostcardSendingService('us', 4, 6);
  });
}
  1. Define the Facade app/Facades/Postcard.php
    • We are grabbing our entire application and we are resolving [$name].
    • In this case we are setting $ name = ‘Postcard’
      • To match the same string we have defined in the AppServiceProvider
    • Inside this Postcard we are calling a dynamic method. That we receive through the callStatic method
      • callStatic() is a PHP magic method that grabs any static method call to this class, when a class doesn’t have a static method with that name.
      • we are calling hello(), but this method it doesn’t exist on this Postcard class
      • the hello() method is inside the service, but notice that this is not static!
 <?php
 
  namespace App/Facades;
 
  class Postcard{
    protected static function resolveFacade($name){
      return $app()[$name];
    }
    
    public static function __callStatic($method, $arguments){
      return self::resolveFacade('Postcard'))
        ->method(...$arguments);
    } 
  }
  1. Call the service using the facade from a Route. 16:26 routes/web.php
    Route::get('/send-a-postcard', function(){
     Postcard::hello('Hello from facade', 'info@test.com');
    });
    

    One more thing. (19:00) We have a file /bootstrap/cache/services.php

    • in this class we have a list
    • this are all aliases