Brief Overview Of Design Patterns Used in Laravel

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

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:

'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:

<?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:

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:

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

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.

  • Pingback: Brief Overview Of Design Patterns Used in Laravel – AngularJS News()

  • This doesn’t look like a real builder pattern to me. The purpose of the builder pattern is to avoid the need for many parameters to object constructors by using a Builder object. Construction is done by parts (see structure here: https://en.wikipedia.org/wiki/Builder_pattern#Structure). But in Laravel that’s just factory methods.

    • Deven Rathore

      Its just compilation of builder pattern laravel uses .

  • Daniel Saavedra

    Nice compilation!

    • Deven Rathore

      thank you

  • Pingback: Brief Overview Of Design Patterns Used in Laravel – Asanka Rathnayake()

  • Carlos Colorado

    For what version of Laravel is this article for ? As someone fairly new to the framework I have no idea how to tell ?
    I can’t find a date to this article either.

    Other than that Article seems nice so far.

    Thanks

    • Deven Rathore

      Its for laravel 5 or higher versions

  • Carlos Colorado

    This is a heads up.
    Today the adds in your site began auto playing videos, every 5 minutes or so.
    I hope this was something you weren’t aware off.

    • Deven Rathore

      thanks for sharing . we have fixed this issue

Pin It on Pinterest

Shares

Get the best in web dev

Join dunebook.com and recieve best in web dev , once a week FREE

An email has been Sent to your Inbox ! Please Confirm your Subscription :)