Welcome Guest, Not a member yet? Register   Sign In
Session Variables Stored in Config File
#1

[eluser]draconus[/eluser]
I have been working with sessions for a little while now, and all works as it should. the only issue I am experiencing is that every time I load a view, I have to set the session variables to an array in order to pass them to the view. Is there a good way to store the variables to a config file so that I don't have to call it with each view?

Here is what I mean:
Code:
$data['firstName'] = $this->session->userdata('firstName');
$this->load->view('member/profile', $data);

It would make better sense, and would probably be the way I want, to pass the session array right into the view, kind of like this:
Code:
$this->load->view('member/profile', $session);
And just set $session = $this->session->userdata(); or something like that in the constructor class, and defaulting it to empty if no session is present.

But if that is a no go, perhaps I can create a config file that would look something like the following:
Code:
$config = array('session' =>
        array(
            'firstName' => $this->session->userdata('firstName'),
            'lastName' => $this->session->userdata('lastName'),
        );

But what would I name the config file for it to access the session variables correctly so I can get this information back to the controller.

In the mean time, I am recreating an array every time I load a view, which is definitely far from DRY.

I have heard recommendations that this can be done within the constructor class, but it complains with an error any time you try to create such an array because it can't call anything from session.

Any hints on how you guys tackle this in your projects would be greatly appreciated.
#2

[eluser]Colin Williams[/eluser]
I don't see how it isn't DRY. Looks like you only grab it once to me. You can instead do $this->load->vars($data) and then it will be available in ALL your view files.
#3

[eluser]draconus[/eluser]
Okay, well i can then set it up in the constructor class like that, or in any of the functions, and load it into vars. That should do it. Great, thanks. I think I need to further look into variable scopes in CI in general, which would have given me the information I needed. I apologize for not thinking of setting it on that level. I will check this out tonight when i get home, but that does seem like it would work.
#4

[eluser]xwero[/eluser]
if you don't use the database option of the session you can always access the session variables using $_COOKIE, that way you don't have to bind them to the view variables array.
#5

[eluser]draconus[/eluser]
I am using the database option. I would assume that this would be setup in the controller constructor class, or better yet, the login class as follows:
Code:
$data['id'] = $this->session->userdata('id');
$data['firstName'] = $this->session->userdata('firstName');
$data['lastName'] = $this->session->userdata('lastName');
$this->load->vars($data)
Then it would be used in other classes normally by simply passing $data into the view, Correct?
#6

[eluser]Colin Williams[/eluser]
You don't need to pass anything to anything any more after loading the vars. If you run that code in the constructor, within any view, you will have $id, $firstName, and $lastName vars.
#7

[eluser]draconus[/eluser]
I understand that, the reason i was thinking of setting it up in a method was because the constructor complains if you put the session objects there. Also, until after the login method is run, there is no session for their to be data for.

Am I still missing something?




Theme © iAndrew 2016 - Forum software by © MyBB