mercredi 17 juillet 2019

Laravel and Auth: Which parameters to pass to controller?

With Laravel's Auth service, you can just call Auth::user() to get the current logged in user and any of their properties. So if you have an MVC with controller functions that use these user properties, is it best practices to not pass the user_id or any other information into the controller, i.e.:

public function getPage($page_number)

as opposed to

public function getPage($page_number, $user_name, $user_type)

or

public function getPage($page_number, $user_id)

The first example makes more sense, since you don't need to pass the user id to get the user, and since you can get the user you don't need to pass its properties. But isn't the first example too tightly coupled? Does the fact that the method signature has to match the route factor into the decision at all?

Aucun commentaire:

Enregistrer un commentaire