• 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
how to retrieve a post_controller_constructor variable in models?

I set value to a language related variable (actually a property) in post_controller_constructor this way:

$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):

$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!

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.

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

Digg   Delicious   Reddit   Facebook   Twitter   StumbleUpon  

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