-
-
Notifications
You must be signed in to change notification settings - Fork 5
Service Container
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.
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.
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 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.
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.
- File-based routing
- Page views
- Page logic
- Dynamic URIs
- Headers and footers
- Custom HTML components
- Page partials
- Binding data to the DOM
- DOM manipulation
- Hello You tutorial
- Todo list tutorial
- Address book tutorial WIP
- Blueprints
- Application architecture
- Coding styleguide WIP
- PHP environment setup WIP
- Web servers WIP
- Background cron tasks
- Database setup WIP
- Client-side compilation WIP
- Testing WebEngine applications WIP
- Production checklist WIP
- Security WIP