• 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Hooks example

#1
[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

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

#3
[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!

#4
[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.

#5
[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.


Digg   Delicious   Reddit   Facebook   Twitter   StumbleUpon  


Users browsing this thread:
1 Guest(s)


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