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

shez1983's avatar

One controller or duplicate n times?

So in my app i currently recreate the same controller depending on User or if its a JSON request so i could have in my COntroller folder:

Admin\UserController; Client\UserController; Resource\UserController;

initially i thought if it was one controller each function would be doing too much - i would have to constantly check which user it is and restrict etc...

But i am not so sure.. sometimes i think this looks messy :s

0 likes
2 replies
tykus's avatar

If the work being done in the various methods depending on the context (admin, client, json) is more than trivial, I would tend towards separating the Controllers as you have done.

The context is important and it can be good to know that whenever you are in a given controller, you know that this is Admin (or Client or Json).

If you are duplicating the same controller actions (including implementation) then consider a BaseUserController, and extend with Admin, Client, Json - this will allow you to override the actions that have specific implementation to that context.

1 like
SapporoGuy's avatar

It all depends on DRY.

If you can keep some functionality together I would. I use repositories for this. I have separated my User Controllers before and do it because it makes sense like @tykus said. However, my difference would be that I call them AdminController, ClientController and EmployeeController (using resourcecontroller here would be confusing since resource has a different meaning in laravel).

Please or to participate in this conversation.