Best Way to Pass Data to All MVC Controllers

Asked

Viewed 491 times

2

After logging in via controller LOGIN, a Session is created with the ID of that user in the system, from that ID can give a "fetch" with all your information, the big problem is that when you log in a navbar is at the top of the site, ex:

Menu principal do site, após fazer login

That navbar is in the archive header.phtml, which is not a "view", but is called when a view is instantiated, to show the navbar of the user on all pages I have to pass a array with that user’s data for all controllers through the main controller in which all extend, so they will "play" this information on header.phtml, what would be the best way to pass logged user information to all controllers and display it in views

  • Do you want to know in which format you pass the data or something else? I really didn’t understand your need.

  • You could go through Session as well and set the data in your controller leading

  • Which framework you are using?

  • I’m doing it by hand, I thought I’d pass a Session with an array, but if the data were updated? would it be static.

  • Inserts a rule on updating data where if the logged-in user updates their own information the session is also updated

  • I get it, can you shed some light on what that rule would be? would it be something like Affectrows? and how would I pass on that information? let’s say that the user updates their data in the 'My Account' controller, as I would 'Warn' another controller?

  • in the method you perform the user update you have to do a if($_SESSION["Usuario"]["id"]){$_SESSION["Usuario"]["username"] = $_POST["Usuario"]["username"]} would be something along those lines, but without seeing their codes it gets very complicated to help more.

  • For now I will indicate your question to close as "it is not clear what you are asking", but if you edit it and make more evident your need I remove my vote.

  • Okay, I think that’s enough, thanks for the help!

Show 4 more comments

2 answers

1

This will depend a lot on how you are organizing your application. If you were using some framework it would be easier to suggest a solution. Follow below possible solutions.

Option 1

Create an object that represents the user and can be accessed anywhere in the application. Something like Seuframewok::$user. In each "page load", initialize this object from the attributes you saved in the session during login. Update the session attributes when the user updates their information. This solution is used by Yiiframework (it has a globally accessible Yii::app()->user object).

Option 2

Place an object that represents the user on the parent controller (also by clicking from the session) on a public access property. Access this object on header.phtml through the reference you have to the controller.

One way or another you need to record user data in a session. The interesting thing is to think of a way to make this easily available for access in your application. I recommend using a framework, because usually it already implements it for you and also a good experience if you want to develop your.

1

I would create a "user" class, with its attributes and properties, and play in a Session. I would overwrite Session whenever any update happened.

A problem of this solution would be the cost that these "Sessions" would have for the server, because imagine an application with more than ten thousand simultaneous accesses where each user would have his "Session" allocated on the server.

  • Let’s say it’s a big app, so what would be the best option?

  • If the object is not complex the cost will not be great. If the object is complex save in the session only a part of it (serialize only the attributes that need to be saved) and load from the session data.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.