Brief Overview Of Design Patterns Used in Laravel

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

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.

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:

   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:

   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

 

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.

About the author

Deven Rathore

Deven Rathore

I'm Deven Rathore, a multidisciplinary & self-taught designer with 3 years of experience. I'm passionate about technology, music, coffee, traveling and everything visually stimulating. Constantly learning and experiencing new things.

  • 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