Welcome Guest, Not a member yet? Register   Sign In
Best Practice Question: Views and Session Vars
#1

[eluser]Kevin Kaske[/eluser]
I have a variable that is stored in a session that I want to show on every view (it's the page title). There is only three ways I can think of...

1. Put logic into the views that loads the var from the session. I don't think this is a good idea.

2. Every time I load a view pass the var into the view. Kind of a pain but it would work.

3. Do something with hooks to replace a string into the resulting view with the value of the session var. Seems like it would be to much overhead.



I'm leaning towards #2, but I just wanted to see if there was an easier way.
#2

[eluser]xwero[/eluser]
You could make a language file titles_lang.php where you add all your titles and in the view file you can do
Code:
<?php echo $this->lang->line('somepagetitle'); ?>
This way you centralize your page titles and you don't have to add anything to the controllers.
#3

[eluser]Kevin Kaske[/eluser]
True. That would work. I was also wanting to make it dynamic. I would like to show the currently logged in user's username.
#4

[eluser]xwero[/eluser]
Then i would use a session variable for two reasons

- it's only visible for that particular user
- if the session is timed out the username shouldn't be visible anymore.

If it's data from a database you could set a config variable.
#5

[eluser]Kevin Kaske[/eluser]
Like I said:

Quote:I have a variable that is stored in a session that I want to show on every view (it’s the page title).

I agree with you. I want to use a session variable. I am just wondering the best way to display it on the view without having to pass it to the view in every function in every controller (Which is what I'm doing right now).

I guess I don't know about config variables. I will look into those.

Thanks for your help!
#6

[eluser]xwero[/eluser]
If it's the username get it when the login check is ok.
#7

[eluser]Rick Jolly[/eluser]
Config variables are for application-wide data, not for a user/session specific data. You can use this->load->vars() to load your variable from the session so that it can be accessed in the view. You could do it in the constructor of every controller, or you could do it once in the constructor of a base/parent/application controller and have all other controllers extend it.
#8

[eluser]nate_02631[/eluser]
It's slightly bad form, but you could just fetch the session variable directly from the view, no?
Code:
<?= $this->session->userdata('username') ?>
#9

[eluser]Kevin Kaske[/eluser]
That is the conflict... Should I stick to the pure MVC model or should I try to make it easy. I think I'm going to stick to pure MVC. What can I say? I come from a Java background. I like to do things the hard way :-)

I will extend the controller with a sub-controller and in turn have the application controllers extend that controller. So basically I will have CI_Base->Controller->Subcontroller->all my other controllers.
#10

[eluser]Kevin Kaske[/eluser]
It seems not possible unless we change the core according to this thread: http://ellislab.com/forums/viewthread/61888/

I guess I will go with plan B and call the session variable in the view. All good design is out the window at this point :-P




Theme © iAndrew 2016 - Forum software by © MyBB