blob: 65696f6c78d3a3fe7f94b2c755984ec0ad36bd43 [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
Derek Jonesa1360ef2011-10-05 17:22:53 -050029 $hook['pre_controller'] = array(
30 'class' => 'MyClass',
31 'function' => 'Myfunction',
32 'filename' => 'Myclass.php',
33 'filepath' => 'hooks',
34 'params' => array('beer', 'wine', 'snacks')
35 );
Derek Jones8ede1a22011-10-05 13:34:52 -050036
37**Notes:**
38The array index correlates to the name of the particular hook point you
39want to use. In the above example the hook point is pre_controller. A
40list of hook points is found below. The following items should be
41defined in your associative hook array:
42
43- **class** The name of the class you wish to invoke. If you prefer to
44 use a procedural function instead of a class, leave this item blank.
45- **function** The function name you wish to call.
46- **filename** The file name containing your class/function.
47- **filepath** The name of the directory containing your script. Note:
48 Your script must be located in a directory INSIDE your application
49 folder, so the file path is relative to that folder. For example, if
50 your script is located in application/hooks, you will simply use
51 hooks as your filepath. If your script is located in
52 application/hooks/utilities you will use hooks/utilities as your
53 filepath. No trailing slash.
54- **params** Any parameters you wish to pass to your script. This item
55 is optional.
56
57Multiple Calls to the Same Hook
58===============================
59
60If want to use the same hook point with more then one script, simply
61make your array declaration multi-dimensional, like this::
62
Derek Jonesa1360ef2011-10-05 17:22:53 -050063 $hook['pre_controller'][] = array(
64 'class' => 'MyClass',
65 'function' => 'Myfunction',
66 'filename' => 'Myclass.php',
67 'filepath' => 'hooks',
68 'params' => array('beer', 'wine', 'snacks')
69 );
70
71 $hook['pre_controller'][] = array(
72 'class' => 'MyOtherClass',
73 'function' => 'MyOtherfunction',
74 'filename' => 'Myotherclass.php',
75 'filepath' => 'hooks',
76 'params' => array('red', 'yellow', 'blue')
77 );
Derek Jones8ede1a22011-10-05 13:34:52 -050078
79Notice the brackets after each array index::
80
81 $hook['pre_controller'][]
82
83This permits you to have the same hook point with multiple scripts. The
84order you define your array will be the execution order.
85
86Hook Points
87===========
88
89The following is a list of available hook points.
90
91- **pre_system**
92 Called very early during system execution. Only the benchmark and
93 hooks class have been loaded at this point. No routing or other
94 processes have happened.
95- **pre_controller**
96 Called immediately prior to any of your controllers being called.
97 All base classes, routing, and security checks have been done.
98- **post_controller_constructor**
99 Called immediately after your controller is instantiated, but prior
100 to any method calls happening.
101- **post_controller**
102 Called immediately after your controller is fully executed.
103- **display_override**
104 Overrides the _display() function, used to send the finalized page
105 to the web browser at the end of system execution. This permits you
106 to use your own display methodology. Note that you will need to
107 reference the CI superobject with $this->CI =& get_instance() and
108 then the finalized data will be available by calling
109 $this->CI->output->get_output()
110- **cache_override**
111 Enables you to call your own function instead of the
112 _display_cache() function in the output class. This permits you to
113 use your own cache display mechanism.
114- **post_system**
115 Called after the final rendered page is sent to the browser, at the
116 end of system execution after the finalized data is sent to the
117 browser.
118