Refactoring #2: From controllers to actions

Take the controller head-on

Our use case is simple: a user wants to join a team. Here’s the controller method that we want to refactor.

Refactor into FormRequests

There’s one more thing we should do with our controller though. We can extract that form validation into its own FormRequest. Helps the readability a bit.

Use Actions in other places as well

And the best part is, we wanted to use the same functionality on the API for our mobile app. It had some differences in how we output results, so we had to use another controller. Sure enough, actions helped in the process. Here’s how the API method looks like with actions:



Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store
Geni Jaho

Geni Jaho

Full-stack web developer with a passion for software architecture and programming best practices.