Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

mystique's avatar

Best design practices for controllers

I have a Laravel app that has grown beyond its initial scope, and I want to refactor it.

The app has a student table that stores individual student information for students in an IT degree. The app then has four other tables that store information about each student's C panel account, confluence account, jira account, and bitbucket account.

In the student controller, I query multiple models, and in the student view, I display a table that contains data from multiple models that I build in the student controller using DataTables. I also use the student controller to create new records in the other database tables when a new student is added.

I have too much logic in the student controller and am about to refactor it into a service class. However, this has me thinking about design practices for controllers.

When I created my app, I created migrations, seeders, models, and controllers for students, cPanel accounts, Jira accounts, Confluence accounts, and Bitbucket accounts.

From a design practice point of view, is it OK for the student controller to query and update multiple models in addition to students?

Or should I have created an additional controller for this purpose?

Thanks!

0 likes
5 replies
NoLAstNamE's avatar

Watch the talk made by Adam Wathan many years ago. It's called "Cruddy by Design".

mystique's avatar

@NoLAstNamE just watched it and that makes more sense than all of the custom actions I have in my controller. Thanks!

1 like
Ben Taylor's avatar

I think a good place to start with controller conventions is to stick with the 7 resource methods (index, show, edit, update, create, store and destroy). If you find yourself wanting to add a different function then it is probably a clue that you need a new controller. I don't think it's necessarily bad to update multiple models in one controller. It probably depends on the situation, but often you'll update related models as part of updating the main model.

mystique's avatar

@Ben Taylor Yes I just watched Cruddy by design and will update my app to use multiple controllers with resource methods. WIll be better than the custom actions I have currently. Thanks for the feedback.

Please or to participate in this conversation.