Welcome Guest, Not a member yet? Register   Sign In
how to retrieve a post_controller_constructor variable in models?
#1

[eluser]Unknown[/eluser]
I set value to a language related variable (actually a property) in post_controller_constructor this way:

Code:
$ci =& get_instance();
...
$ci->lngcode = some_value;

I can retrieve this variable in views: $this->lngcode, but it is not possible in models, as $this->lngcode throws an error here. Unfortunately, i need this value in a model.

I am relatively new to CI so my first question is: why $this->lngcode is wrong here in the model?

Is it OK (a good practice?) to retrieve this variable by referencing the CI superobject in the model (i tested and it works):

Code:
$ci =& get_instance();
...
echo $ci->lngcode // do something with the value

or should i pass this value in the controller as a parameter to the model's method?

Thx for help!
#2

[eluser]jedd[/eluser]
Hi teamtom and welcome to the CI forums.

I understand that in general terms you should avoid using get_instance like this - from memory it's a (relatively) expensive operation.

You might want to look at using [url="/user_guide/libraries/sessions.html"]Session data[/url] for storing this kind of stuff - especially if it is something that will change based on different user's preferences.
#3

[eluser]Unknown[/eluser]
hi jedd,

thanks for the answer!

i am familiar with the concept of the session
in this case i tried NOT to use anything related to cookies to build a SEO friendly multi language site with langcode (en, de, fr, etc.) in the url

thinking of KISS (keep it simple stupid), you are right, a session variable seems good enough for the job

but what about the first question? why does $this->lngcode throw an error in a model?

TIA, teamtom




Theme © iAndrew 2016 - Forum software by © MyBB