spebon's avatar

Grouping methods by model or actions

Let's say that I have a typical route with create, edit, index, show, store, update and destroy for authenticated users. I now need a route for guests to perform similiar, but not the same, actions on the same model. Should I create a new controller with e.g. create and store? This would result in smaller files and thus easier to read code as well as match Laravel conventions but require that I either duplicate or split out shared code to an abstract class, a trait or a standalone class.

My other option is to add methods like guestCreate and guestStore to the original controller and rename the existing ones to authenticated{Action}. This is more verbose and makes the file much larger resulting in more difficult to read code and might not follow conventions, but I can avoid duplicate code, unnecessarily complicated relationships, external calls and difficult to navigate class structures.

Are there any points I've failed to take into consideration? What is best practice here?

0 likes
1 reply
Sinnbeck's avatar

I would definitely go with the first option. It follows crud conventions and makes the code easier to read.

Please or to participate in this conversation.