CodeIgniter Forums
Hooks example - Printable Version

+- CodeIgniter Forums (https://forum.codeigniter.com)
+-- Forum: Archived Discussions (https://forum.codeigniter.com/forumdisplay.php?fid=20)
+--- Forum: Archived Development & Programming (https://forum.codeigniter.com/forumdisplay.php?fid=23)
+--- Thread: Hooks example (/showthread.php?tid=6105)



Hooks example - El Forum - 02-14-2008

[eluser]freshface[/eluser]
Hey

I want to extend the $this->db class is the best way to use a hook?
And could an example be posted? The user guide is not that revealing for me.

Thx in advance


Hooks example - El Forum - 02-15-2008

[eluser]freshface[/eluser]
Anyone an example?


Hooks example - El Forum - 04-13-2008

[eluser]SabbeRubbish[/eluser]
Hi freshface,
I've been a CI user for only 1 day and I've already needed a hook.
Was scratching the back of my head to figure out how to get the session or db as just using $this-> wouldn't work of course.

So I'll show you how I've done my authentication hook for every page, loaded as post_controller_constructor:

Code:
$hook['post_controller_constructor'] = array(
                                'class'    => 'Hooks',
                                'function' => 'session_check',
                                'filename' => 'hooks.php',
                                'filepath' => 'hooks',
                                'params'   => array()
                                );
Code:
<?php
class Hooks {
    var $CI;

    function Hooks() {
        $this->CI =& get_instance();
    }
    
    function session_check() {
        if(!$this->CI->session->userdata("logged_in") && $this->CI->uri->uri_string != "/user/login")
            redirect('user/login', 'location');
    }
}

?>

Use this at your own risk, I'm still going to change the way my session data is stored (although it's not mission critical and I've got it slightly encrypted, but hey...)

Hope you can use this!


Hooks example - El Forum - 04-13-2008

[eluser]webthink[/eluser]
I think the fact that hooks are labeled in the manual as "extending the core" is a bit misleading. Essentially what they're for are allocating your own custom routines to be run at any of the few predetermined spots in the CI load stack. For people new to CodeIgniter a heading such as "auto running custom code" would make more sense.
For extending the DB libraries it's not a hook you want. Have a look at "Extending native libraries" under the libraries section.


Hooks example - El Forum - 04-14-2008

[eluser]SabbeRubbish[/eluser]
[quote author="webthink" date="1208134805"]I think the fact that hooks are labeled in the manual as "extending the core" is a bit misleading. Essentially what they're for are allocating your own custom routines to be run at any of the few predetermined spots in the CI load stack. For people new to CodeIgniter a heading such as "auto running custom code" would make more sense.
For extending the DB libraries it's not a hook you want. Have a look at "Extending native libraries" under the libraries section.[/quote]

I think the term hook suits perfectly for it, but indeed it doesn't extend the core.