Backend Development 16 min read

Understanding Laravel’s Core Concepts: Lifecycle, Service Container, Dependency Injection, Contracts, and Facades

This article explains Laravel’s underlying architecture by detailing PHP’s execution phases, Laravel’s request lifecycle, the bootstrap process, the service container, dependency injection, binding mechanisms, and the role of contracts and facades, providing practical code examples for each concept.

Architecture Digest
Architecture Digest
Architecture Digest
Understanding Laravel’s Core Concepts: Lifecycle, Service Container, Dependency Injection, Contracts, and Facades

Laravel, a popular PHP backend framework, follows both PHP's own lifecycle (MINIT, RINIT, execution, RSHUTDOWN, MSHUTDOWN) and its own request handling process that starts and ends in public/index.php .

The Laravel request lifecycle can be broken down into four main steps:

require __DIR__.'/../bootstrap/autoload.php';

$app = require_once __DIR__.'/../bootstrap/app.php'; and $kernel = $app->make(Illuminate\Contracts\Http\Kernel::class);

$response = $kernel->handle($request = Illuminate\Http\Request::capture()); followed by $response->send();

$kernel->terminate($request, $response);

During these steps Laravel registers its core services, loads the service container, resolves the incoming request, runs the middleware pipeline, and finally sends the response back to the client.

The service container is a powerful IoC container that stores class instances and resolves dependencies automatically. It enables dependency injection, allowing classes to declare the services they need without manually instantiating them.

Example of a controller using dependency injection:

class UserController extends Controller {
    protected $users;
    public function __construct(UserRepository $users) {
        $this->users = $users;
    }
    public function show($id) {
        $user = $this->users->find($id);
        return view('user.profile', ['user' => $user]);
    }
}

Bindings can be registered in service providers using bind or singleton . The bind closure is executed on each resolution, while singleton creates the instance only once per request.

$this->app->bind('XblogConfig', function ($app) {
    return new MapRepository();
});

$this->app->singleton('XblogCache', function ($app) {
    return config('cache.enable') == 'true' ? new Cacheable() : new NoCache();
});

Laravel also relies heavily on contracts (interfaces) to decouple implementations. By coding against contracts, the framework can swap underlying services without changing application logic.

namespace App\Contracts;
use Closure;
interface XblogCache {
    public function setTag($tag);
    public function setTime($time_in_minute);
    public function remember($key, Closure $entity, $tag = null);
    public function forget($key, $tag = null);
    public function clearCache($tag = null);
    public function clearAllCache();
}

Two concrete implementations illustrate this flexibility:

class Cacheable implements XblogCache {
    public $tag;
    public $cacheTime;
    public function setTag($tag) { $this->tag = $tag; }
    public function setTime($time_in_minute) { $this->cacheTime = $time_in_minute; }
    public function remember($key, Closure $entity, $tag = null) {
        return cache()->tags($tag ?? $this->tag)->remember($key, $this->cacheTime, $entity);
    }
    public function forget($key, $tag = null) { cache()->tags($tag ?? $this->tag)->forget($key); }
    public function clearCache($tag = null) { cache()->tags($tag ?? $this->tag)->flush(); }
    public function clearAllCache() { cache()->flush(); }
}

class NoCache implements XblogCache {
    public function setTag($tag) {}
    public function setTime($time_in_minute) {}
    public function remember($key, Closure $entity, $tag = null) { return $entity(); }
    public function forget($key, $tag = null) {}
    public function clearCache($tag = null) {}
    public function clearAllCache() {}
}

Facades provide a static-like interface to services bound in the container. By defining a facade class that returns the container binding via getFacadeAccessor , developers can call methods statically while the underlying instance is resolved at runtime.

In summary, Laravel’s strength lies in its simple yet extensible core built around the service container, contracts, and facades, allowing developers to write high‑quality, decoupled code while leveraging powerful scaffolding features.

backend developmentphpdependency injectionLaravelFacadesContractsService Container
Architecture Digest
Written by

Architecture Digest

Focusing on Java backend development, covering application architecture from top-tier internet companies (high availability, high performance, high stability), big data, machine learning, Java architecture, and other popular fields.

0 followers
Reader feedback

How this landed with the community

login Sign in to like

Rate this article

Was this worth your time?

Sign in to rate
Discussion

0 Comments

Thoughtful readers leave field notes, pushback, and hard-won operational detail here.