Laravel Facades: How Facades Work

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().

Related concepts

How Facades Work

Laravel Facades: How Facades Work — Structure map

Clickable & Draggable!

Laravel Facades: How Facades Work — Related pages: