In this episode, we're tackling some much-needed refactoring to clean up our controllers—specifically, we're moving validation and authorization logic out of the controller and into form request classes. It might sound dry, but this process does wonders for making controllers clear and maintainable.
We start by extracting validation logic from our "BookPutController" into a dedicated form request, making it clear where our rules live. A bonus of this is that we can handle authorization inside the same form request as well, resulting in super-tidy controllers. While doing this, we double-check our tests to ensure everything's still running as expected after the changes.
Next, we do a similar refactor for the "BookStoreController," creating a separate form request for storing books. We discuss why keeping 'put' and 'store' rules in their own classes, for now, makes future changes easier. Along the way, we see that Laravel's request objects give us everything we need, including root model binding, which makes authorization inside form requests straightforward.
By the end of the video, we've got controllers that are much easier to understand, with validation and authorization logic consolidated in form requests. We wrap things up by re-running our tests to confirm everything works perfectly after refactoring. If you want cleaner controllers and better-organized code, this episode shows you exactly how to achieve that!