Dependency Injection in CakePHP
summary
CakePHP 4.2 introduces an experimental container that registers service definitions, supports deferred loading via service providers (activate only when a listed service is requested), and supplies a service config proxy for read-only config values. Controllers and console commands get constructor injection; action parameters use reflection to inject dependencies. Containers store recipes (shared vs non-shared), resolve dependency graphs, and can be swapped for tests. The feature is flagged experimental, may change before stabilization (targeted for 4.3+), and recommends passing Request objects into service methods rather than injecting them.
AI-written summary · about
chapters (16)
- 0:00Introduction to talk
- 0:42What is dependency injection
- 2:04Constructor injection basics
- 3:31Why dependency injection patterns
- 4:34Factories and registries
- 5:58Locators and their drawbacks
- 8:25Containers explained
- 10:34CakePHP's current DI usage
- 11:59Application logic gap
- 13:02What are application services
- 14:48New container in CakePHP 4.2
- 15:50Service providers and lazy loading
- 17:34Injecting services into controllers
- 18:58Handling request in services
- 19:18Experimental features explained
- 21:43Call for feedback and testing
