In this article , we will discuss the design patterns Laravel uses, and how and why are they used, using some realife Examples . we have divided article To 6 chapters to give you a Brief Overview Of Design Patterns Used in Laravel .

  1. Builder pattern
  2. Factory pattern
  3. Repository pattern
  4. Strategy pattern
  5. Provider pattern
  6. Facade Pattern

<

div class=”itemizedlist”>

<

div class=”titlepage”>

The Builder pattern

This design pattern aims to gain simpler, reusable objects. Its goal is to separate bigger and more convoluted object construction layers from the rest so that the separated layers can be used in different layers of the application.

<

div class=”section” title=”The need for the Builder (Manager) pattern”>

<

div class=”titlepage”>

The need for the Builder pattern

In Laravel, the AuthManager class needs to create some secure elements to reuse with selected auth storage drivers such as cookie, session, or custom elements. To achieve this, the AuthManager class needs to use storage functions such as callCustomCreator() and getDrivers() from theManager class.

Let’s see how the Builder (Manager) pattern is used in Laravel. To see what happens in this pattern, navigate to the vendor/Illuminate/Support/Manager.php andvendor/Illuminate/Auth/AuthManager.php files, as shown in the following code:

<

div class=”informalexample”>

   public function driver($driver = null)
   {
      ...

   }

   protected function createDriver($driver)
   {
      $method = 'create'.ucfirst($driver).'Driver';

      ...
   }

   protected function callCustomCreator($driver)
   {
      return $this->customCreators[$driver]($this->app);
   }

   public function extend($driver, Closure $callback)
   {
      $this->customCreators[$driver] = $callback;

      return $this;
   }
   public function getDrivers()
   {
      return $this->drivers;
   }

 public function __call($method, $parameters)
   {
      return call_user_func_array(array($this->driver(), $method), $parameters);
   }

Now, navigate to the /vendor/Illuminate/Auth/AuthManager.php file, as shown in the following code:

<

div class=”informalexample”>

   protected function createDriver($driver)
   {

     ....
   }

   protected function callCustomCreator($driver)
   {


   }

   public function createDatabaseDriver()
   {


   }

   protected function createDatabaseProvider()
   {

      ....
   }


   public function createEloquentDriver()
   {
      ...

   }

   protected function createEloquentProvider()
   {
      ...

   }

   public function getDefaultDriver()
   {
      ...
   }

   public function setDefaultDriver($name)
   {
      ...
   }

As we can see in the preceding code, the AuthManager class extends from the Manager class. Laravel ships with a basic auth mechanism. So, we need to store auth credentials in a database. First, the class checks our default database configuration with the AuthManager::setDefaultDriver()function. This function actually uses the Manager class for eloquent operations. All the database and auth options (such as cookie name) are obtained from the application’s config file, except the auth model table name.

To understand this Builder (Manager) pattern better, we can take the following presentation as an example:

 

builder

 

<

div class=”mediaobject”>

<

div class=”mediaobject”>

In the preceding example diagram, we assumed that we want to fetch data, for example, pizza, from the previous illustration. The client ordered two pizzas: an Asian pizza and/or a Chinese pizza. This pizza is requested through the Waiter class. The PizzaBuilder class (which is the Manager class, in our case) made a pizza according to the AuthRequest request, in our case, and delivered the pizza through the waiter.Also, you can navigate to vendor/Illuminate/Session/SessionManage.php and check for the use of this pattern in Laravel Framework.