PHP Classes

Laravel Service Container Example: Resolve class dependencies using an IoC container

Recommend this page to a friend!
  Info   View files Documentation   View files View files (64)   DownloadInstall with Composer Download .zip   Reputation   Support forum   Blog    
Last Updated Ratings Unique User Downloads Download Rankings
2022-05-03 (4 months ago) RSS 2.0 feedNot yet rated by the usersTotal: 19 All time: 10,731 This week: 246Up
Version License PHP version Categories
laravel-service-cont 1.0The PHP License5PHP 5, Libraries, Language, Design Pa...
Description Author

This package demonstrates how to resolve class dependencies using an Inversion of Control as a Laravel service container.

It provides an example application that demonstrates how to set up a service container that can solve dependencies of classes on each other without using separate configurations.

The package demonstrates how to create a container class that dynamically solves the dependencies of services.

Picture of Nahidul Hasan
  Performance   Level  
Name: Nahidul Hasan <contact>
Classes: 11 packages by
Country: Bangladesh Bangladesh
Age: ???
All time rank: 279836 in Bangladesh Bangladesh
Week rank: 15 Up1 in Bangladesh Bangladesh Up
Innovation award
Innovation award
Nominee: 7x

Details

Laravel Service Container

>An IoC Container is a convenience Mechanism for achieving Dependency Injection -Taylor Otwell

Laravel is one of the most popular, highly used, open-source modern web application framework. It provides unique features like Eloquent ORM, Query builder ,Homestead which are the modern features, only present in Laravel.

I like Laravel because of its unique architectural design.Behind the scene Laravel uses different design pattern such as Singleton, Factory, Builder, Facade, Strategy, Provider, Proxy etc. So when my knowledge is increasing, I am finding its beauty. Laravel makes developer?s life more easy and removes boringness.

Learning code with Laravel is not just about learning to use the different classes but also learning the philosophy of Laravel, its elegance and its beautiful syntax. An important part of Laravel?s philosophy is the IoC container or Service Container. Understanding and using the IoC container is a crucial part in mastering our craft, as it is the core part of a Laravel application.

Service Container is a powerful tool for managing class dependencies and performing dependency injection. It has the power to automatically resolve classes without configuration. Here I will try to discuss why we need it and How it works.

If at first we know Dependency Inversion Principle it will help us to understand why we need Service Container. So in the beginning I will discuss Dependency Inversion Principle.

The principle states:

>High-level modules should not depend on low-level modules. Both should depend on abstractions.

> Abstractions should not depend on details. Details should depend on abstractions.

Or simply : Depend on Abstractions not on concretions

class MySQLConnection
{
   /
   * db connection
   */
   public function connect()
   {
      var_dump(?MYSQL Connection?);
   }
}

class PasswordReminder
{    
    /
     * @var MySQLConnection
     */
     private $dbConnection;
    public function __construct(MySQLConnection $dbConnection) 
    {
      $this->dbConnection = $dbConnection;
    }
}

There?s a common misunderstanding that dependency inversion is simply another way to say dependency injection. However, the two are not the same.In the above code Inspite of Injecting MySQLConnection class in PasswordReminder class but it is depends on MySQLConnection.

High-level module PasswordReminder should not depend on low-level module MySQLConnection.

If we want to change connection from MySQLConnection to MongoDBConnection, we have to change hard coded constructor injection in PasswordReminder class.

PasswordReminder class should depend upon on Abstractions not on concretions. But How can we do it ? Please see the following example :

interface ConnectionInterface
{
   public function connect();
}
class DbConnection implements ConnectionInterface
{
 /
  * db connection
  */
 public function connect()
 {
   var_dump(?MYSQL Connection?);
 }
}
class PasswordReminder
{
    /
    * @var DBConnection
    */
    private $dbConnection;
    public function __construct(ConnectionInterface $dbConnection)
    {
      $this->dbConnection = $dbConnection;
    }
}

In the above code we want to change connection from MySQLConnection to MongoDBConnection, we no need to change constructor injection in PasswordReminder class.Because here PasswordReminder class depends upon on Abstractions not on concretions.

If your concept is not clear about interface then you can read this doc . This doc will help you to understand Dependency Inversion Principle, IoC container etc clearly.

Now I will discuss what happens in IoC container. we can simply say that IoC container is a Container that contains Inversion of Control (dependencies of a class).

OrderRepositoryInterface :

namespace App\Repositories;
interface OrderRepositoryInterface 
{
   public function getAll();
}

DbOrderRepository class:

namespace App\Repositories;
class DbOrderRepository implements OrderRepositoryInterface
{
 
  function getAll()
  {
    return 'Getting all from mysql';
  }
}

OrdersController class:


namespace App\Http\Controllers;
use Illuminate\Http\Request;
use App\Http\Requests;
use App\Repositories\OrderRepositoryInterface;
class OrdersController extends Controller
{
    protected $order;
   function __construct(OrderRepositoryInterface $order)
   {
     $this->order = $order;
   }
    
   public function index()
   {
     dd($this->order->getAll());
     return View::make(orders.index);
   }
}

Routing:

Route::resource('orders', 'OrdersController');

Now if you hit browser using this url http://localhost:8000/orders

Yow will get this error .Because container is trying to instantiate the interface. We can fix that by creating a specific binding for our interface.

BindingResolutionException in Container.php line 748: Target [App\Repositories\OrderRepositoryInterface] is not instantiable while building [App\Http\Controllers\OrdersController].

Just adding this line code in route page we can solve it.

App::bind('App\Repositories\OrderRepositoryInterface', 'App\Repositories\DbOrderRepository');

Now if you hit browser you will get :

"Getting all from mysql"

Here, Need to mention that, We should not resolve app bind in route page. Here I have added only for example purpose. In our professional project we should have resolve app binding issue in `AppServiceProviderclassregister` method as described below:

$this->app->bind('App\Repositories\OrderRepositoryInterface', 'App\Repositories\DbOrderRepository');

we can define a container class in following way:

class SimpleContainer
 {
    protected static $container = [];
    public static function bind($name, Callable $resolver)
    {   
        static::$container[$name] = $resolver;
    }
    public static function make($name)
    {
      if(isset(static::$container[$name])){
        $resolver = static::$container[$name] ;
        return $resolver();
    }
    throw new Exception("Binding does not exist in containeer");
   }
}

Here I will try to show that how simple container resolves dependency


class LogToDatabase 
{
    public function execute($message)
    {
       var_dump('log the message to a database :'.$message);
    }
}
class UsersController {
    
    protected $logger;
    
    public function __construct(LogToDatabase $logger)
    {
        $this->logger = $logger;
    }
    
    public function show()
    {
      $user = 'JohnDoe';
      $this->logger->execute($user);
    }
}

Here bind dependency.


SimpleContainer::bind('Foo', function()
 {
   return new UsersController(new LogToDatabase);
 });
$foo = SimpleContainer::make('Foo');
print_r($foo->show());

Output :

string(36) "Log the messages to a file : JohnDoe"

Laravel?s container code :

public function bind($abstract, $concrete = null, $shared = false)
    {
        $abstract = $this->normalize($abstract);
        $concrete = $this->normalize($concrete);
        if (is_array($abstract)) {
           list($abstract, $alias) = $this->extractAlias($abstract);
           $this->alias($abstract, $alias);
        }
        $this->dropStaleInstances($abstract);
        if (is_null($concrete)) {
            $concrete = $abstract;
        }

        if (! $concrete instanceof Closure) {
            $concrete = $this->getClosure($abstract, $concrete);
        }
        
        $this->bindings[$abstract] = compact('concrete', 'shared');

        if ($this->resolved($abstract)) {
            $this->rebound($abstract);
        }
    }
public function make($abstract, array $parameters = [])
    {
        $abstract = $this->getAlias($this->normalize($abstract));
        if (isset($this->instances[$abstract])) {
            return $this->instances[$abstract];
        }
      $concrete = $this->getConcrete($abstract);
        if ($this->isBuildable($concrete, $abstract)) {
            $object = $this->build($concrete, $parameters);
        } else {
            $object = $this->make($concrete, $parameters);
        }

        foreach ($this->getExtenders($abstract) as $extender) {
            $object = $extender($object, $this);
        }
        if ($this->isShared($abstract)) {
            $this->instances[$abstract] = $object;
        }
       $this->fireResolvingCallbacks($abstract, $object);
       $this->resolved[$abstract] = true;
       return $object;
    }
public function build($concrete, array $parameters = [])
    {
        
        if ($concrete instanceof Closure) {
            return $concrete($this, $parameters);
        }
       $reflector = new ReflectionClass($concrete);
        if (! $reflector->isInstantiable()) {
            if (! empty($this->buildStack)) {
                $previous = implode(', ', $this->buildStack);
        $message = "Target [$concrete] is not instantiable while building [$previous].";
            } else {
                $message = "Target [$concrete] is not instantiable.";
            }
          throw new BindingResolutionException($message);
        }
         $this->buildStack[] = $concrete;
         $constructor = $reflector->getConstructor();
        if (is_null($constructor)) {
            array_pop($this->buildStack);
           return new $concrete;
        }
        $dependencies = $constructor->getParameters();
        $parameters = $this->keyParametersByArgument(
            $dependencies, $parameters
        );
     $instances = $this->getDependencies($dependencies,$parameters);
     array_pop($this->buildStack);
     return $reflector->newInstanceArgs($instances);
    }


If you want to know more details all method about container then you can see

vendor/laravel/framwork/src/Illuminate/Container/Container.php

Simple Bindings


$this->app->bind('HelpSpot\API', function ($app) {
    return new HelpSpot\API($app->make('HttpClient'));
});

Binding A Singleton

The singleton method binds a class or interface into the container that should only be resolved one time.

$this->app->singleton('HelpSpot\API', function ($app) {
    return new HelpSpot\API($app->make('HttpClient'));
});



Binding Instances

You may also bind an existing object instance into the container using the instance method. The given instance will always be returned on subsequent calls into the container:


$api = new HelpSpot\API(new HttpClient);

$this->app->instance('HelpSpot\API', $api);

If there is no binding, PHP?s Reflection class is used to resolve the instance and dependencies.

You can learn more about it by reading the docs

I have published this article in the medium. if you?d like to read from the medium blog site, please go to this link

Thank you for reading.

  Files folder image Files  
File Role Description
Files folder imageapp (1 file, 7 directories)
Files folder imagebootstrap (2 files)
Files folder imageconfig (12 files)
Files folder imagedatabase (3 directories)
Files folder imagepublic (4 files)
Files folder imageresources (3 directories)
Files folder imagetests (2 files)
Accessible without login Plain text file .env.example Data Auxiliary data
Plain text file artisan Class Class source
Accessible without login Plain text file composer.json Data Auxiliary data
Accessible without login Plain text file composer.lock Data Auxiliary data
Accessible without login Plain text file gulpfile.js Data Auxiliary data
Accessible without login Plain text file index.php Example Example script
Accessible without login Plain text file package.json Data Auxiliary data
Accessible without login Plain text file phpunit.xml Data Auxiliary data
Accessible without login Plain text file readme.md Doc. Documentation
Accessible without login Plain text file server.php Aux. Auxiliary script

  Files folder image Files  /  app  
File Role Description
Files folder imageConsole (1 file, 1 directory)
Files folder imageEvents (1 file)
Files folder imageExceptions (1 file)
Files folder imageHttp (2 files, 3 directories)
Files folder imageJobs (1 file)
Files folder imageProviders (4 files)
Files folder imageRepositories (2 files)
  Plain text file User.php Class Class source

  Files folder image Files  /  app  /  Console  
File Role Description
Files folder imageCommands (1 file)
  Plain text file Kernel.php Class Class source

  Files folder image Files  /  app  /  Console  /  Commands  
File Role Description
  Plain text file Inspire.php Class Class source

  Files folder image Files  /  app  /  Events  
File Role Description
  Plain text file Event.php Class Class source

  Files folder image Files  /  app  /  Exceptions  
File Role Description
  Plain text file Handler.php Class Class source

  Files folder image Files  /  app  /  Http  
File Role Description
Files folder imageControllers (2 files, 1 directory)
Files folder imageMiddleware (4 files)
Files folder imageRequests (1 file)
  Plain text file Kernel.php Class Class source
  Accessible without login Plain text file routes.php Aux. Auxiliary script

  Files folder image Files  /  app  /  Http  /  Controllers  
File Role Description
Files folder imageAuth (2 files)
  Plain text file Controller.php Class Class source
  Plain text file OrdersController.php Class Class source

  Files folder image Files  /  app  /  Http  /  Controllers  /  Auth  
File Role Description
  Plain text file AuthController.php Class Class source
  Plain text file PasswordController.php Class Class source

  Files folder image Files  /  app  /  Http  /  Middleware  
File Role Description
  Plain text file Authenticate.php Class Class source
  Plain text file EncryptCookies.php Class Class source
  Plain text file RedirectIfAuthenticated.php Class Class source
  Plain text file VerifyCsrfToken.php Class Class source

  Files folder image Files  /  app  /  Http  /  Requests  
File Role Description
  Plain text file Request.php Class Class source

  Files folder image Files  /  app  /  Jobs  
File Role Description
  Plain text file Job.php Class Class source

  Files folder image Files  /  app  /  Providers  
File Role Description
  Plain text file AppServiceProvider.php Class Class source
  Plain text file AuthServiceProvider.php Class Class source
  Plain text file EventServiceProvider.php Class Class source
  Plain text file RouteServiceProvider.php Class Class source

  Files folder image Files  /  app  /  Repositories  
File Role Description
  Plain text file DbOrderRepository.php Class Class source
  Plain text file OrderRepositoryInterface.php Class Class source

  Files folder image Files  /  bootstrap  
File Role Description
  Plain text file app.php Class Class source
  Accessible without login Plain text file autoload.php Aux. Auxiliary script

  Files folder image Files  /  config  
File Role Description
  Plain text file app.php Class Class source
  Plain text file auth.php Class Class source
  Accessible without login Plain text file broadcasting.php Aux. Auxiliary script
  Accessible without login Plain text file cache.php Aux. Auxiliary script
  Accessible without login Plain text file compile.php Aux. Auxiliary script
  Accessible without login Plain text file database.php Aux. Auxiliary script
  Accessible without login Plain text file filesystems.php Aux. Auxiliary script
  Accessible without login Plain text file mail.php Aux. Auxiliary script
  Accessible without login Plain text file queue.php Aux. Auxiliary script
  Plain text file services.php Class Class source
  Accessible without login Plain text file session.php Aux. Auxiliary script
  Accessible without login Plain text file view.php Aux. Auxiliary script

  Files folder image Files  /  database  
File Role Description
Files folder imagefactories (1 file)
Files folder imagemigrations (2 files)
Files folder imageseeds (1 file)

  Files folder image Files  /  database  /  factories  
File Role Description
  Plain text file ModelFactory.php Class Class source

  Files folder image Files  /  database  /  migrations  
File Role Description
  Plain text file 2014_10_12_000000_create_users_table.php Class Class source
  Plain text file 2014_10_12_100000_...rd_resets_table.php Class Class source

  Files folder image Files  /  database  /  seeds  
File Role Description
  Plain text file DatabaseSeeder.php Class Class source

  Files folder image Files  /  public  
File Role Description
  Accessible without login Plain text file .htaccess Data Auxiliary data
  Plain text file index.php Class Class source
  Accessible without login Plain text file robots.txt Doc. Documentation
  Accessible without login Plain text file web.config Data Auxiliary data

  Files folder image Files  /  resources  
File Role Description
Files folder imageassets (1 directory)
Files folder imagelang (1 directory)
Files folder imageviews (1 file, 1 directory)

  Files folder image Files  /  resources  /  assets  
File Role Description
Files folder imagesass (1 file)

  Files folder image Files  /  resources  /  assets  /  sass  
File Role Description
  Accessible without login Plain text file app.scss Data Auxiliary data

  Files folder image Files  /  resources  /  lang  
File Role Description
Files folder imageen (4 files)

  Files folder image Files  /  resources  /  lang  /  en  
File Role Description
  Accessible without login Plain text file auth.php Aux. Auxiliary script
  Accessible without login Plain text file pagination.php Aux. Auxiliary script
  Accessible without login Plain text file passwords.php Aux. Auxiliary script
  Accessible without login Plain text file validation.php Aux. Auxiliary script

  Files folder image Files  /  resources  /  views  
File Role Description
Files folder imageerrors (1 file)
  Accessible without login Plain text file welcome.blade.php Aux. Auxiliary script

  Files folder image Files  /  resources  /  views  /  errors  
File Role Description
  Accessible without login Plain text file 503.blade.php Aux. Auxiliary script

  Files folder image Files  /  tests  
File Role Description
  Plain text file ExampleTest.php Class Class source
  Plain text file TestCase.php Class Class source

 Version Control Unique User Downloads Download Rankings  
 100%
Total:19
This week:0
All time:10,731
This week:246Up
For more information send a message to info at phpclasses dot org.