retaining a value in a controller variable

Hello Developers

I am stuck at a design issue.

I have a controller class which has a variable
var id
It is set in a function A and is required in all the
functions in this controller (lets say B, C and D)

Now functions B,C,D and called for my views which leads to
reinitialization of the controller and loss of id which was
set in function A.

The only solution I see is to pass the id to my views and then
again to all the functions B, C and D whenever they are called.

But I guess is this is not the best way to do it. Is there any way
I can set the value of id and retain it till its changed the next time.


As you say, one idea is to pass them as parameters in the URL. If you wish to "hide" the variable value a better solution is to use sessions. Read up on it here:

session and cookies are one way but then this is not session data. It is an id and the best way would
be to have a system to retain this value in my controller as I move between my controller and views.

So is there anyway ?

If it is a value that might differ between requests (that is, it is not a constant), as I see it you've got those two options, either pass them publicly in your URL via links or privately, using sessions, a database or file writes.

Session data is whatever data you keep in a session, so I don't see a reason why not to keep it there. Anyway, your description of the problem is a bit vague, so if you wish a more detailed answer please expand on exactly how you wish things to work.

ok, I am trying to setup a forum. So basically I am passing the topic id to this controller with handles all the comments for this topic.
Based on the topic id I load the view showing all the comments and for any new comments posted on this view, I am directed back to the same controller and I need to store the comment with the topic id.
What I want to do is store the topic id so that I don't need to pass it back and forth while I traverse between controller and view.
And clearly this is not a session data since I may open a lot of topics and hence I cannot override this data

Agreed. I would go with the link approach then, since one might want to bookmark these different views.

Digg   Delicious   Reddit   Facebook   Twitter   StumbleUpon  

  Theme © 2014 iAndrew  
Powered By MyBB, © 2002-2020 MyBB Group.