Back

I was watching a talk recently about testing your code (it might have been @nunomaduro 's talk on Pest 3?) that mentioned that your controllers should ONLY have the resource / CRUD methods in them.

I'm building a more complex app that has routes related to a user taking a test that's doing a lot more than just those methods. If not in a controller, where should those methods be placed?

I have a TestSessionController that handles all of the routes for the next question and grading the test and the questions. I am moving a lot of the functionality out of the controller and into Action classes, but I still use different methods in my controller to call those actions.

Or was that just an example of what could be done, and not necessarily a "best practice" sort of thing? Any help would be great!

3

245

  • No matching results...
  • Searching...

/ 1000

It was an example but it's also a common patten. Adam Wathan gave a talk cruddy by design in 2017 where he says only have those methods in your class, once you need more create another class.

www.youtube.com/watch?v=MF0jFKvS4SI

1

173

Thanks for the video! I'll check it out tonight.

1

125

@dcblogdev

Thank you so much for that video. I wish I had watched this a few months ago and maybe this project I’m wrapping up could have been made cleaner.

I don’t know if I would want to make EVERYTHING CRUD but it definitely gives me ideas in how to clean things up.

1

289

I always love these type of videos, can change how you build applications.

471

First thing if your project is old and running from long it's okay to not update those methods.

So remember one sentence before anything else 'if you need other methods than these golden 7 methods means you need another controller.

Now let's see one example.

You have PostController and you already have 7 methods in it. And now you need 2 methods which make post draft and published. So you can create PublishedPostController and put store/update for publishing the post and destroy for making it draft.

🦹🦹

198

Personally, I have mixed feelings about this principle. I don’t like all facets of ‘Cruddy by Design’ or what it does to your code. I usually stick to resourceful controllers, and anything that doesn’t belong there gets its own invokable controller with a clear, descriptive name.

116