Best way to pass data to all MVC Controllers [closed]

2

After logging in by the controller LOGIN , a session is created with the ID of this user on the system, from that ID fetch "with all your information, the big problem is that when logging the user a navbar is at the top of the site, eg:

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

    
asked by anonymous 22.05.2014 / 21:39

2 answers

1

I would create a "user" class with its attributes and properties, and would play in a session. I would overwrite the session every time an update happens.

One problem with this solution would be the cost of these sessions for the server, so imagine an application with more than ten thousand simultaneous accesses where each user would have his / her session allocated to the server.

    
23.05.2014 / 20:00
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. Below are possible solutions.

Option 1

Create an object that represents the user and can be accessed anywhere in the application. Something like SeuFramewok :: $ user . On each page load, initialize this object from the attributes that you recorded in the session during login. Update the attributes of the session 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 representing the user on the parent controller (also loading from the session), in a public access property. Access this object in header.phtml through the reference you have to the controller.

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

    
23.05.2014 / 00:24