Welcome Guest, Not a member yet? Register   Sign In
What exactly gets passed to the hook?
#1

[eluser]oddman[/eluser]
I'm trying to figure out exactly how to use the hooks so that I can check for session information upon every page request. I come from a cakephp background and it's incredibly easy in that, as they provide parent methods that allow you to execute code at any point. I'm wanting to execute a hook before the controller is called. Hints/tips on how I could do this - and what will be available to the class that I will write to handle this hook? Also, will the hook be able to set variables.etc. in the controller? Unfortunately there isn't enough documentation for this stuff =\
#2

[eluser]oddman[/eluser]
*bump*

Does anyone know?

I'd like to be able to access controller info.etc.
#3

[eluser]gtech[/eluser]
well if you want to set variables in the hook i have just written a test for you.

hook:
Code:
<?php  if (!defined('BASEPATH')) exit('No direct script access allowed');
class MyClass
{
    public function MyFunction($p1)
    {
        $ci = & get_instance();
        $ci->testv = 'hi';
    }
}
?>

controller
Code:
<?php
class Temp extends Controller {

  function Temp()
  {
    parent::Controller();
    $this->load->library('session');
  }
  function index()
  {
    echo $this->testv;
  }
}
?>

the temp controller printed out hi

the hook was defined as
Code:
$hook['post_controller_constructor'] = array(
  'class'    => 'MyClass',
  'function' => 'Myfunction',
  'filename' => 'Myclass.php',
  'filepath' => 'hooks',
  'params'   => array('anything'));
?>

I loaded it as post constructor so I could get the CI instance to set variables. In theory anything you put in the constructor should be available in the hook using the CI instance.

is that what you are after? (dont forget to enable hooks in the config file)
#4

[eluser]oddman[/eluser]
Bingo! Ty gtech Smile The get_instance() function is what i was after, brilliant.

Thanks again!
#5

[eluser]gtech[/eluser]
no probs, glad to help.
#6

[eluser]oddman[/eluser]
hmmm, funnily enough it doesn't seem to be executing the code - despite having hooks enabled =\
#7

[eluser]gtech[/eluser]
ok I got it working on mine..

did you place the hook in the hooks directory with the correct filename and classname?
and did you place the config array in the config/hooks.php file within the php tags.
did you ensure the hook had php tags around it (that was my mistake at first).

try cut and pasting my example it does work honest guv Smile
#8

[eluser]oddman[/eluser]
yeah, even tried different naming conventions. Isn't returning any errors =\
#9

[eluser]gtech[/eluser]
did you cut and paste my example or did you use your own? my original example had a ; in the get_instance, which the forum inserted.. I edited the above script so it had a space in between the & and get, which works when executing the code.

you could try upping your log_threshold see if you get any errors in the log then.
#10

[eluser]oddman[/eluser]
hmmm, that didn't help. Error logging set to 0.




Theme © iAndrew 2016 - Forum software by © MyBB