• 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Using PHP Session and the CI Session together

#1
[eluser]laegnur[/eluser]
Hello

I'm still learning step by step, the workings of CodeIgniter. Now I'm triying this. I have a controller, that extends from my_controller class.

Code:
class Inicio extends MY_Controller
{
  function Inicio('inicio')
  {
    parent::MY_Controller();  
  }

  function index()
  {
    $this->load->view('view_inicio');
  }
}

In My_controller class, I obtain some data from a model, that extends from my_model class (but it's other history... ), and I set a config item with this data.

Code:
<?php if(!defined('BASEPATH')) exit('No direct script access allowed');
class MY_Controller extends Controller
{
  function My_Controller($star = 'inicio')
  {
    parent::Controller();
    if(!$this->uri->segment(1))
    {
      redirect($star,'location');
    }
    $this->config->set_item('theme', $this->model_config->get('name','theme')->value);
  }
}
/* End of file MY_Controller.php */
/* Location: ./application/libraries/MY_Controller.php */

I can access this config value from the first controller's view. But when I go to other controller (that not extend from MY_Controller class) clicking a link in the first controller's view, I can't acces this config value from this second controller's view.

Code:
<?php echo $this->config->item('theme'); ?>

Am I incorrectly setting configuration item?

With this I intend to only have to perform the query to the DB just once



P.d.: Sorry for my ugly english

#2
[eluser]Georgi Budinov[/eluser]
Code:
function My_Controller($star = 'inicio')

to

Code:
function CA_Controller($star = 'inicio')

should be fine!

#3
[eluser]laegnur[/eluser]
Hello.

That was a typo. I'm using the subclass prefix CA_, but for avoid confusions, I published here using MY_

#4
[eluser]n0xie[/eluser]
Quote:I can access this config value from the first controller’s view. But when I go to other controller (that not extend from MY_Controller class) clicking a link in the first controller’s view, I can’t acces this config value from this second controller’s view.
The config item isn't actually 'written' to file. It is just set for that request. Hence on a new request where you don't explicitly set the config item (any controller not extending MY_Controller), the config item won't exist.

#5
[eluser]laegnur[/eluser]
Hello

So, if I correctly understand, the only way to keep that value for all controls is through session

#6
[eluser]n0xie[/eluser]
Session, cookie, or do a DB lookup on each request.

#7
[eluser]Philipp Gérard[/eluser]
Or simply write a config file using PHP.

#8
[eluser]Bart v B[/eluser]
[quote author="Philipp Gérard" date="1282673989"]Or simply write a config file using PHP.[/quote]

Can you give an example for that? I mean the CI way?

#9
[eluser]bretticus[/eluser]
Ummm, this is for remembering the user's theme it appears. Are you trying to avoid a database? Why store this as a config item? Are you programing an installer?

#10
[eluser]bretticus[/eluser]
[quote author="Laegnur" date="1282659306"]
With this I intend to only have to perform the query to the DB just once
[/quote]

Ah, this certainly is a good case for storing in a session. I don't think you want to write a flat file here for that. If you plan on storing a lot of data in sessions, I recommend enabling database sessions.

If you really wanna avoid sessions (even though this seems like the perfect case for sessions) there is always app caching alternatives such as APC or Memcache.


Digg   Delicious   Reddit   Facebook   Twitter   StumbleUpon  


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