DEV Community

Ghulam Mujtaba
Ghulam Mujtaba

Posted on

1

Service Container Bootstrap: Getting Started

Before starting the topic, create two new files in controllers directory:

  1. destroy.php that contains the code used to delete a note from the database.
  2. store.php that contains the code to create a new note using forms and request methods.

Intro to Service Container

A Service Container is a design pattern that centralizes the management of dependencies between objects, providing a structured way to access and manage services within an application.

It acts as a registry for services within a website.

Container Class

Firstly create a file named Controller.php in Core directory. In this initialize the Container class, that is responsible for managing service bindings and resolutions.

  • bind(): Registers a service with the container.
  • resolve(): Retrieves a service from the container.
class Container {
    protected $bindings = [];

    public function bind($key, $resolver) {
        $this->bindings[$key] = $resolver;
    }

    public function resolve($key) {
        if (!array_key_exists($key, $this->bindings)) {
            throw new Exception("No matching binding found for {$key}");
        }
        $resolver = $this->bindings[$key];
        return call_user_func($resolver);
    }
}
Enter fullscreen mode Exit fullscreen mode

App Class

The App class acts as a interface for the Container, providing a convenient interface for accessing services.

  • setContainer(): Sets the container instance for the App.
  • container(): Retrieves the container instance.
  • bind(): Registers a service with the container.
  • resolve(): Retrieves a service from the container.
class App {
    protected static $container;

    public static function setContainer($container) {
        static::$container = $container;
    }

    public static function container() {
        return static::$container;
    }

    public static function bind($key, $resolver) {
        static::container()->bind($key, $resolver);
    }

    public static function resolve($key) {
        return static::container()->resolve($key);
    }
}
Enter fullscreen mode Exit fullscreen mode

Bootstrap

Bootstrap is a point of an application, where everything is set up and initialized.

$container = new Container();
$container->bind('Core\Database', function () {
    $config = require base_path('config.php');
    return new Database($config['database']);
});
App::setContainer($container);
Enter fullscreen mode Exit fullscreen mode

In this , the bootstrap process:

  1. Creates a new Container instance ($container)
  2. Registers services with the container using the bind method (e.g., Core\Database)
  3. Sets the container for the App using App::setContainer($container)

Controller

Services can be resolved from the container in controllers using the App::resolve() method.

$db = App::resolve('Core\Database');
Enter fullscreen mode Exit fullscreen mode

Now a service container is properly built and you can see the project is working well.

I hope that you have clearly understood it.

Sentry blog image

How to reduce TTFB

In the past few years in the web dev world, we’ve seen a significant push towards rendering our websites on the server. Doing so is better for SEO and performs better on low-powered devices, but one thing we had to sacrifice is TTFB.

In this article, we’ll see how we can identify what makes our TTFB high so we can fix it.

Read more

Top comments (0)

Sentry image

See why 4M developers consider Sentry, “not bad.”

Fixing code doesn’t have to be the worst part of your day. Learn how Sentry can help.

Learn more

👋 Kindness is contagious

Please leave a ❤️ or a friendly comment on this post if you found it helpful!

Okay