Skip to content
Greg Bowler edited this page May 11, 2026 · 3 revisions

The service container is the part of WebEngine that constructs and shares dependencies for the current request. Instead of manually building every object in page logic, we can ask for the service we need and let the container provide it.

The standalone service container package is documented in more detail at https://www.php.gt/docs/ServiceContainer/Home/.

Lazy loading is an important part of this. A service does not need to be constructed until something actually asks for it. That keeps the request lighter, especially when many pages only use a small subset of the available services.

Default Service Loader

WebEngine's default service loader provides the services that most pages are likely to need, including:

  • config
  • request and response state
  • input and URI data
  • database-related services
  • binding-related services for the HTML document

This is why a page logic function can simply ask for something like Input, Session, or Database and expect it to be there already.

Service loading strategy

The default service loader covers the framework's normal wiring, but an application can also provide its own service loader class. That is where project-specific services can be registered.

The container then keeps a single instance of each service for the request. If two different parts of the code ask for the same dependency, they receive the same shared object rather than two unrelated copies.

Good uses for the container

Good uses include:

  • shared infrastructure services
  • helpers built from configuration
  • repositories, factories, and other application-level services

These are exactly the kinds of things that page logic should not have to construct repeatedly by hand.

Pitfalls

The container should not become a vague global registry for everything. If arbitrary state is hidden in the container and fetched from anywhere, we lose the clarity that dependency injection is meant to give us.

Prefer services with clear responsibilities and explicit type-hinted dependencies.


Now we know how to load services in our page logic, learn how and where to build application classes.

Clone this wiki locally