DEV Community

Mbo
Mbo

Posted on • Updated on

Question for PHP/Laravel developers.

What rules of thumb(personal or industry based) do you follow when building applications in Laravel?

Top comments (3)

Collapse
 
alchermd profile image
John Alcher • Edited

I follow this rule: Controllers are strictly for resource I/O, so a controller at max can only have seven public methods (one for each Restful actions). If it's not one of the following

ArticleController@index
ArticleController@create
ArticleController@store
ArticleController@show
ArticleController@edit
ArticleController@update
ArticleController@delete

... I move it to a separate controller. So let's say an article can be upvoted; instead of a custom ArticleController@upvote method, I create an UpvoteController@store instead. Take note that an upvote is not a concrete Eloquent model, but it can still be considered a resource.

TL;DR: A Controller doesn't necessarily have to map with an Eloquent model. Use it to group resource-related logic so you don't have to write custom controller methods.


Edit: I may have butchered that concept, I'm quite tired and can't type properly lol.

Collapse
 
mubarakky profile image
Mbo

Nice!. Please add a number so we can all keep track of the rules. LOL!

Collapse
 
mubarakky profile image
Mbo

OK, I'll go first:

  1. To aid in the logical organization of my code, I link a set of features to related table(s) and logic such that it ends up having corresponding model(s), controller(s) and views. For example, say I have a set of features called "Admin {CRUD}" I'll create a table called "users" or "admins", then I'll create a model called "User" or "Admin", then a controller called "AdminController" with my logic going into the controller.