In this episode, we take a closer look at how to resolve (or grab) action classes from the container, rather than creating them directly in your controllers. This is a small tweak but it brings a lot more clarity and maintainability to your code, especially when your project starts growing.
We'll start by showing how you might typically use an action straight inside a controller, but then we refactor it to inject the action—like CreatePost
—directly from the container instead. This approach not only tidies up how dependencies are managed, but also sets us up for cleaner and more testable code.
We discuss some naming conventions to keep your code readable (naming the injected action exactly what it is), and you'll see how this pattern shines if you need to use the action in multiple places, like inside a class constructor, or if you're grabbing it for another service.
Lastly, we walk through updating the controller to use constructor property promotion (if your PHP version supports it) to make dependency injection even cleaner. Everything still works as before, but now the code reads better, is easier to test, and sets up a solid pattern for future development.