blob: ab42d28a14a97fdf8fe28f5544a3e7c298779e99 [file] [log] [blame]
Derek Jones8ede1a22011-10-05 13:34:52 -05001####################################
2Hooks - Extending the Framework Core
3####################################
4
5CodeIgniter's Hooks feature provides a means to tap into and modify the
6inner workings of the framework without hacking the core files. When
7CodeIgniter runs it follows a specific execution process, diagramed in
8the :doc:`Application Flow <../overview/appflow>` page. There may be
9instances, however, where you'd like to cause some action to take place
10at a particular stage in the execution process. For example, you might
11want to run a script right before your controllers get loaded, or right
12after, or you might want to trigger one of your own scripts in some
13other location.
14
15Enabling Hooks
16==============
17
18The hooks feature can be globally enabled/disabled by setting the
19following item in the application/config/config.php file::
20
21 $config['enable_hooks'] = TRUE;
22
23Defining a Hook
24===============
25
26Hooks are defined in application/config/hooks.php file. Each hook is
27specified as an array with this prototype::
28
29 $hook['pre_controller'] = array(                                 'class'    => 'MyClass',                                 'function' => 'Myfunction',                                 'filename' => 'Myclass.php',                                 'filepath' => 'hooks',                                 'params'   => array('beer', 'wine', 'snacks')                                 );
30
31**Notes:**
32The array index correlates to the name of the particular hook point you
33want to use. In the above example the hook point is pre_controller. A
34list of hook points is found below. The following items should be
35defined in your associative hook array:
36
37- **class** The name of the class you wish to invoke. If you prefer to
38 use a procedural function instead of a class, leave this item blank.
39- **function** The function name you wish to call.
40- **filename** The file name containing your class/function.
41- **filepath** The name of the directory containing your script. Note:
42 Your script must be located in a directory INSIDE your application
43 folder, so the file path is relative to that folder. For example, if
44 your script is located in application/hooks, you will simply use
45 hooks as your filepath. If your script is located in
46 application/hooks/utilities you will use hooks/utilities as your
47 filepath. No trailing slash.
48- **params** Any parameters you wish to pass to your script. This item
49 is optional.
50
51Multiple Calls to the Same Hook
52===============================
53
54If want to use the same hook point with more then one script, simply
55make your array declaration multi-dimensional, like this::
56
57 $hook['pre_controller'][] = array(                                 'class'    => 'MyClass',                                 'function' => 'Myfunction',                                 'filename' => 'Myclass.php',                                 'filepath' => 'hooks',                                 'params'   => array('beer', 'wine', 'snacks')                                 ); $hook['pre_controller'][] = array(                                 'class'    => 'MyOtherClass',                                 'function' => 'MyOtherfunction',                                 'filename' => 'Myotherclass.php',                                 'filepath' => 'hooks',                                 'params'   => array('red', 'yellow', 'blue')                                 );
58
59Notice the brackets after each array index::
60
61 $hook['pre_controller'][]
62
63This permits you to have the same hook point with multiple scripts. The
64order you define your array will be the execution order.
65
66Hook Points
67===========
68
69The following is a list of available hook points.
70
71- **pre_system**
72 Called very early during system execution. Only the benchmark and
73 hooks class have been loaded at this point. No routing or other
74 processes have happened.
75- **pre_controller**
76 Called immediately prior to any of your controllers being called.
77 All base classes, routing, and security checks have been done.
78- **post_controller_constructor**
79 Called immediately after your controller is instantiated, but prior
80 to any method calls happening.
81- **post_controller**
82 Called immediately after your controller is fully executed.
83- **display_override**
84 Overrides the _display() function, used to send the finalized page
85 to the web browser at the end of system execution. This permits you
86 to use your own display methodology. Note that you will need to
87 reference the CI superobject with $this->CI =& get_instance() and
88 then the finalized data will be available by calling
89 $this->CI->output->get_output()
90- **cache_override**
91 Enables you to call your own function instead of the
92 _display_cache() function in the output class. This permits you to
93 use your own cache display mechanism.
94- **post_system**
95 Called after the final rendered page is sent to the browser, at the
96 end of system execution after the finalized data is sent to the
97 browser.
98