Laravel Facades

Laravel Facades

A facade is a class that provides access to an object from the container.

Provide a "static" interface to classes that are available in the application's service container.

Use Illuminate\Support\Facades\Cache;

Route::get('/cache', function () {
    return Cache::get('key');
});
  • Laravel ships with many facades which provide access to almost all of framework's features.
  • Serve as "static proxies" to underlying classes in the service container, providing the benefit of a terse, expressive syntax while maintaining more testability and flexibility than traditional static methods.
  • Defined in the Illuminate\Support\Facades namespace.
  • Provide a terse, memorable syntax that allows you to use Laravel's features without remembering long class names that must be injected or configured manually.
  • Furthermore, because of their unique usage of PHP's dynamic methods, they are easy to test.
  • When using its, pay special attention to the size of your class so that its scope of responsibility stays narrow.
  • Use dynamic methods to proxy method calls to objects resolved from the service container.
  • We actually can test its just as we would test an injected class instance.
  • Extend the base Illuminate\Support\Facades\Facade class.
  • Its base class makes use of the __callStatic() magic-method to defer calls from your facade to an object resolved from the container.
  • They provide a terse, memorable syntax that allows you to use Laravel's features without remembering long class names that must be injected or configured manually.
  • Are so easy to use and do not require injection, it can be easy to let your classes continue to grow and use many facades in a single class.

Additional information

  • The primary danger of its is class scope creep.
In most cases, each of them has an equivalent contract.

Access a facade

Use Illuminate\Support\Facades\Cache;

Route::get('/cache', function () {
    return Cache::get('key');
});

When To Use Facades

Pay special attention to the size of your class so that its scope of responsibility stays narrow.

How Facades Work

<?php

namespace App\Http\Controllers;

use App\Http\Controllers\Controller;
use Illuminate\Support\Facades\Cache;

class UserController extends Controller
{
    /**
     * Show the profile for the given user.
     *
     * @param  int  $id
     * @return Response
     */
    public function showProfile($id)
    {
        $user = Cache::get('user:'.$id);

        return view('profile', ['user' => $user]);
    }
}
Class Cache extends Facade
{
    /**
     * Get the registered name of the component.
     *
     * @return string
     */
    protected static function getFacadeAccessor() { return 'cache'; }
}
  • Any custom facades you create, will extend the base Illuminate\Support\Facades\Facade class.
  • The Facade base class makes use of the __callStatic() magic-method to defer calls from your facade to an object resolved from the containe.
  • Any calls we make using the facade will be passed to the underlying instance of Laravel's cache service.
  • Instead, the Cache facade extends the base Facade class and defines the method getFacadeAccessor().

Facade Class Reference

Laravel Facades — Structure map

Clickable & Draggable!

Laravel Facades — Related pages: