Brief Overview Of Design Patterns Used in Laravel

The Provider pattern

The Provider pattern was formulated by Microsoft for use in the ASP.NET Starter Kits and formalized in .NET Version 2.0 (http://en.wikipedia.org/wiki/Provider_model). It is a mid layer between an API class and the Business Logic/Data Abstraction Layer of the application. The provider is the implementation of the API separated from the API itself.

This pattern, its aims, and its usage are quite similar to the Strategy pattern. This is why many developers are already discussing whether to accept this approach as a design pattern.

To understand these patterns better, let’s openvendor/Illuminate/Auth/AuthServiceProvider.php andvendor/Illuminate/Hashing/HashServiceProvider.php:

<?php namespace Illuminate\Auth;

use Illuminate\Support\ServiceProvider;

class AuthServiceProvider extends ServiceProvider {

   protected $defer = true;

   public function register()
   {
      $this->app->bindShared('auth', function($app)
      {
           // Once the authentication service has actually been requested by the developer
          // we will set a variable in the application indicating this, which helps us
          // to know that we need to set any queued cookies in the after event later.
         $app['auth.loaded'] = true;

          return new AuthManager($app);
      });
   }

   public function provides()
   {
      return array('auth');
   }

}


<?php namespace Illuminate\Hashing;

use Illuminate\Support\ServiceProvider;

class HashServiceProvider extends ServiceProvider {

   protected $defer = true;

   public function register()
   {
      $this->app->bindShared('hash', function() { return new BcryptHasher; });
   }

   public function provides()
   {
      return array('hash');
   }

}

As you can see, both the classes extend ServiceProvider. The AuthServiceProvider class allows us to provide all services to AuthManager when an authentication request, such as checking whether a cookie and session is created or whether the content is invalid, is sent. After the authentication service has been requested, the developer can define whether a session or cookie is set through the response through AuthDriver.

However, HashServiceProvider provides us with the related methods when a secure hash request is done so that we can use, fetch, check, or do other things with these hashes. Both providers return the values as an array.

About the author

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

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