In this episode, we switch things up by removing our traditional controller and instead start using an action class directly as a controller with the help of the Laravel Actions package. First, we walk through what happens if you try treating your action like a controller out of the box (hint: you'll run into method errors), and then we see how to fix this by adding the asController
method to your action class.
We experiment a bit—debug what's going on, use die dump
to trace the execution, and figure out how the package expects you to wire things up. You’ll see how the asController
method serves as the entry point when the action is used as a route handler, and how it can call the handle
method with the necessary data, like the authenticated user and request data. The approach starts to make sense as we get things working and realize we now have an action that is both reusable and can double as a controller method!
To wrap up, we mention one important thing that’s still missing: request data validation inside the action. So, we tee things up for the next episode, where we'll tackle model validation directly in the action method. Stay tuned if you want your actions to properly validate incoming data!