<

div class=”titlepage”>

The Facade pattern

The Facade (façade) pattern allows a developer to unite various complicated interfaces into a single class interface. This pattern also allows you to wrap various methods from various classes into a single structure.

facade

<

div class=”mediaobject”>

In Laravel 4, as you may already know, almost every method looks like a static method, for example,Input::has(), Config::get(), URL::route(), View::make(), and HTML::style(). However, they are not static methods. If they were static methods, it would be quite hard to make tests for them all. They are actually the mimics of this behavior. In the background, with the help of the IoC Container (a way to inject dependencies into a class), Laravel actually calls another class(es) through a Facade class. The Facade base class benefits from PHP’s own __callStatic() magic method to call the required methods, such as static methods.For example, let’s assume we have a method called URL::to('home'). Let’s check what the URL is and what it refers to. First, let’s open app/config/app.php. In the aliases array, there is a line like the following:

<

div class=”informalexample”>

'URL' => 'Illuminate\Support\Facades\URL',

So, if we call URL::to('home'), we actually call Illuminate\Support\Facades \URL::to('home').

Now, let’s check what’s inside the file. Open thevendor/Illuminate/Support/Facades/URL.php file:

<

div class=”informalexample”>

<?php namespace Illuminate\Support\Facades;

class URL extends Facade {


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

}

As you can see, the class actually extends from the Facade class, and there is no static method calledto(). Instead, there is a method called getFacadeAccessor(), which returns the string url. ThegetFacadeAccessor() method’s purpose is to define what to inject. This way, Laravel understands that this class is looking for $app['url'].

This is defined in vendor/Illuminate/Routing/RoutingServiceProvider.php, as follows:

<

div class=”informalexample”>

protected function registerUrlGenerator()
{
   $this->app['url'] = $this->app->share(function($app)
      {

      $routes = $app['router']->getRoutes();

      return new UrlGenerator($routes, $app->rebinding('request', function($app, $request)
      {
         $app['url']->setRequest($request);
      }));
   });
}

As you can see, it returns a new instance of the UrlGenerator class in the same namespace, which holds the to() method we’re looking for:

<

div class=”informalexample”>

//Illuminate/Routing/UrlGenerator.php
public function to($path, $extra = array(), $secure = null)
{
   //...
}

So each time you use a method like this, Laravel first goes to and checks the facade, it then checks what’s injected through, and then the real method through the injected class is called.

<

div class=”titlepage”>

Wrapping up

In this article , we learned about various design pattern uses in the Laravel PHP framework, how and why they are used, and what problems they can solve.