Welcome Guest, Not a member yet? Register   Sign In
possible to use URI library in pre_controller hook?
#1

[eluser]the real rlee[/eluser]
Hi, this may be a silly question but is it possible to use the URI library in a pre_controller hook? I'm getting errors telling me the object doesnt exist, but the User Docs indicate that the URI class is pre-loaded with CI and pre_controller hooks are after everything CI is loaded... Am I missing something here? Appreciate any help Big Grin

Code:
$hook['pre_controller'] = array('class'=>'History', 'function'=>'update', 'filename'=>'History.php', 'filepath'=>'hooks', 'params'=>array());

And my hook:
Code:
class History {

var $CI;

function History(){
  $this->CI =& get_instance();
}

function update(){
  die($this->CI->uri->uri_string());
  $this->CI->session->set_userdata(array('location'=>$this->CI->uri->uri_string()));
}

}
#2

[eluser]the real rlee[/eluser]
Wow nobody knows ?

Essentially what im trying to do is recreate javascript's History object so i can redirect users around my site. Obviously sessions will be required, but im not sure how i can record each page visit automatically without literally coding it into each controller, which is why i was thinking of using a pre_controller hook to record the uri string.

Anyway ideas greatly appreciated!
#3

[eluser]the real rlee[/eluser]
Hmmm this is what im doing right now, for some reason the history session array isn't being updated i.e. it keeps create a new history item...Any ideas?

Code:
// Save current location
  $history = $this->CI->session->userdata('history');
  //echo ($this->CI->session->userdata('history') ? $this->CI->session->userdata('history') : 'FALSE').'<br >';
  
  if (!is_array($history)) {
   echo 'creating history array...';
   $this->CI->session->set_userdata('history', array('blah'));
   $history = $this->CI->session->userdata('history');
  
  }
  // limit the history array to 4
  if (count($history) > 4) {
   echo 'removing old history item...';
   array_shift($history);
  }
  
  array_push($history, $this->CI->uri->uri_string());
  $this->CI->session->set_userdata('history', $history);
  
  echo '<pre>'; print_r($this->CI->session->userdata('history')); echo '</pre>';
#4

[eluser]Shadi[/eluser]
Hello,

in the pre_controller hook, you won't have the CI object yet under get_instance()

so a solution to use CI resources in a pre_controller hook is to do something like this:

Code:
class History {

var $CI;

function History(){
  // doesn't matter since we are using a pre_controller hook
  $this->CI =& get_instance();
}

function update(){
  global $RTR , $URI, $CFG , $LANG;
  // $RTR stands for the router library
  // $URI stands for the URI library
  // $CFG stands for Config library
  // $LANG stands for Language Library

  // instead of  die($this->CI->uri->uri_string());
  // use:
  die($URI->uri->uri_string());

  // the following code won't work since session library isn't loaded yet in pre_controller.
  $this->CI->session->set_userdata(array('location'=>$this->CI->uri->uri_string()));
}

}

those four objects are the available objects in the pre_controller.

Hope this helps Smile
#5

[eluser]the real rlee[/eluser]
Ahh thanks Shadi! Wish that was in the User Guide
#6

[eluser]Hamilogre[/eluser]
I tried this, and it didn't seem to work. Does it work for anyone else?
#7

[eluser]instantations[/eluser]
[quote author="the real rlee" date="1185378183"]Wish that was in the User Guide[/quote]

+1




Theme © iAndrew 2016 - Forum software by © MyBB