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.

Chapter 5 of 6