CodeIgniter Forums
How does a hook work? - Printable Version

+- CodeIgniter Forums (https://forum.codeigniter.com)
+-- Forum: Archived Discussions (https://forum.codeigniter.com/forum-20.html)
+--- Forum: Archived Development & Programming (https://forum.codeigniter.com/forum-23.html)
+--- Thread: How does a hook work? (/thread-5412.html)



How does a hook work? - El Forum - 01-21-2008

[eluser]doors[/eluser]
I am writing a post controller constructor hook to run some application wide code.

What I need to know though is if the hook creates a object of the class I pass to the hook array so that I can use back that object in my controllers?


If yes. What is the name of the object?


How does a hook work? - El Forum - 01-21-2008

[eluser]nirbhab[/eluser]
i am using the same post controller constructor hook, so it loads some default values for my site like title of the page, for that purpose i have used define,

but for accessing the core libraries of CI use the following
Code:
$CI =& get_instance();
i am using CI's object.


How does a hook work? - El Forum - 01-21-2008

[eluser]nirbhab[/eluser]
here is sample hook, hope it helps you out.

Code:
<?php
if (!defined('BASEPATH')) exit('No direct script access allowed');
class getSystem
{
    function check()
    {
$CI =& get_instance();
$CI->load->helper('date');
//below code in not tested, according to concept, but above is running fine.
$CI->value="some_data";
//hope you can access this data in controller or model or view by $this->value
    }
}
?>



How does a hook work? - El Forum - 01-21-2008

[eluser]doors[/eluser]
After getting the access to the CI object how can I get to use the the hook has created for my class in my controllers?

I am not sure how to do that.


How does a hook work? - El Forum - 01-21-2008

[eluser]nirbhab[/eluser]
when we are extending the CI's instance, means its object.
Code:
$this->variable_name_declared_in_hook    
//in controller, as we are extending our controllers by Controller class
class Blog extends Controller{
// look at user guide
}
try this
Code:
print_r($this)
//u will get loooooong list of array, containing whole list of data. check it.
paste your hook code so that, i could understand it more easily, what u r up to.


How does a hook work? - El Forum - 01-21-2008

[eluser]doors[/eluser]
[quote author="nirbhab" date="1200946434"]here is sample hook, hope it helps you out.

Code:
<?php
if (!defined('BASEPATH')) exit('No direct script access allowed');
class getSystem
{
    function check()
    {
$CI =& get_instance();
$CI->load->helper('date');
//below code in not tested, according to concept, but above is running fine.
$CI->value="some_data";
//hope you can access this data in controller or model or view by $this->value
    }
}
?>
[/quote]


The $CI->value="some_data"; above will create that $value variable automatically within the CI object so that I can access it anywhere the CI variable is accessible?


How does a hook work? - El Forum - 01-21-2008

[eluser]doors[/eluser]
When I use print_r in my post_controller_constructor class it prints nothing but when I put it in the controller it prints out alot of info.


How does a hook work? - El Forum - 01-21-2008

[eluser]doors[/eluser]
[quote author="nirbhab" date="1200946763"]
paste your hook code so that, i could understand it more easily, what u r up to.[/quote]

I have not written the hook as yet.

That's what I am trying to do now, but I need some info first.