I'm learning how to work with the MVC pattern, using Laravel, but I have a question. This is an example case, there is a User model and the user can register by itself or an admin can create a new user account using the admin panel, the user should be logged in after the registration and the admin redirected to the admin panel home or something like that. I see 3 possible ways to do this:
Make 2 controllers for the same model, UserController and AdminUserController.
Use only 1 controller with 2 separate methods, UserCreate and Admin Create.
Use only 1 controller with only a create method and check the user role to do the correct actions depending of it.
I don't know if any of them is correct; I would appreciate any opinion about which is the best approach.
1 Answer 1
You have two distinct interactions with your system:
- An anonymous user performs a user registration and, as a successful result, becomes a registered, logged-in, user.
- A logged-in administrator creates a new user account
As these are distinct interactions with similar but different responses from the system, you should be using 2 controllers to handle these interactions.
In the model, you probably don't want to make a distinction how a user got created (if it was done by an admin or the user himself), so this points to having just a single class in the model for users.
Note that in the MVC pattern, there is a single Model part, which consists of all classes that are not related to user interactions, and that Model is shared between all Controllers and Views.