blob: ffe3fef3983ba7de8cd7b09f58ed569272e01fa5 [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
Andrey Andreev16a704c2012-11-09 17:25:00 +020019following item in the **application/config/config.php** file::
Derek Jones8ede1a22011-10-05 13:34:52 -050020
21 $config['enable_hooks'] = TRUE;
22
23Defining a Hook
24===============
25
Andrey Andreev83514042014-03-06 00:28:55 +020026Hooks are defined in the **application/config/hooks.php** file.
Andrey Andreev16a704c2012-11-09 17:25:00 +020027Each hook is specified as an array with this prototype::
Derek Jones8ede1a22011-10-05 13:34:52 -050028
Derek Jonesa1360ef2011-10-05 17:22:53 -050029 $hook['pre_controller'] = array(
Andrey Andreev16a704c2012-11-09 17:25:00 +020030 '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:**
Andrey Andreev16a704c2012-11-09 17:25:00 +020038
Derek Jones8ede1a22011-10-05 13:34:52 -050039The array index correlates to the name of the particular hook point you
40want to use. In the above example the hook point is pre_controller. A
41list of hook points is found below. The following items should be
42defined in your associative hook array:
43
44- **class** The name of the class you wish to invoke. If you prefer to
45 use a procedural function instead of a class, leave this item blank.
Andrey Andreev16a704c2012-11-09 17:25:00 +020046- **function** The function (or method) name you wish to call.
Derek Jones8ede1a22011-10-05 13:34:52 -050047- **filename** The file name containing your class/function.
Andrey Andreev16a704c2012-11-09 17:25:00 +020048- **filepath** The name of the directory containing your script.
49 Note:
50 Your script must be located in a directory INSIDE your *application/*
51 directory, so the file path is relative to that directory. For example,
52 if your script is located in *application/hooks/*, you will simply use
53 'hooks' as your filepath. If your script is located in
54 *application/hooks/utilities/* you will use 'hooks/utilities' as your
Derek Jones8ede1a22011-10-05 13:34:52 -050055 filepath. No trailing slash.
56- **params** Any parameters you wish to pass to your script. This item
57 is optional.
58
Andrey Andreev83514042014-03-06 00:28:55 +020059If you're running PHP 5.3+, you can also use lambda/anoymous functions
60(or closures) as hooks, with a simpler syntax::
61
62 $hook['post_controller'] = function()
63 {
64 /* do something here */
65 };
66
Derek Jones8ede1a22011-10-05 13:34:52 -050067Multiple Calls to the Same Hook
68===============================
69
Andrey Andreevba231aa2014-01-20 16:43:41 +020070If want to use the same hook point with more than one script, simply
Derek Jones8ede1a22011-10-05 13:34:52 -050071make your array declaration multi-dimensional, like this::
72
Derek Jonesa1360ef2011-10-05 17:22:53 -050073 $hook['pre_controller'][] = array(
Andrey Andreev16a704c2012-11-09 17:25:00 +020074 'class' => 'MyClass',
75 'function' => 'MyMethod',
76 'filename' => 'Myclass.php',
77 'filepath' => 'hooks',
78 'params' => array('beer', 'wine', 'snacks')
79 );
Derek Jonesa1360ef2011-10-05 17:22:53 -050080
81 $hook['pre_controller'][] = array(
Andrey Andreev16a704c2012-11-09 17:25:00 +020082 'class' => 'MyOtherClass',
83 'function' => 'MyOtherMethod',
84 'filename' => 'Myotherclass.php',
85 'filepath' => 'hooks',
86 'params' => array('red', 'yellow', 'blue')
87 );
Derek Jones8ede1a22011-10-05 13:34:52 -050088
89Notice the brackets after each array index::
90
91 $hook['pre_controller'][]
92
93This permits you to have the same hook point with multiple scripts. The
94order you define your array will be the execution order.
95
96Hook Points
97===========
98
99The following is a list of available hook points.
100
101- **pre_system**
102 Called very early during system execution. Only the benchmark and
103 hooks class have been loaded at this point. No routing or other
104 processes have happened.
105- **pre_controller**
106 Called immediately prior to any of your controllers being called.
107 All base classes, routing, and security checks have been done.
108- **post_controller_constructor**
109 Called immediately after your controller is instantiated, but prior
110 to any method calls happening.
111- **post_controller**
112 Called immediately after your controller is fully executed.
113- **display_override**
Andrey Andreev16a704c2012-11-09 17:25:00 +0200114 Overrides the ``_display()`` method, used to send the finalized page
Derek Jones8ede1a22011-10-05 13:34:52 -0500115 to the web browser at the end of system execution. This permits you
116 to use your own display methodology. Note that you will need to
Andrey Andreev16a704c2012-11-09 17:25:00 +0200117 reference the CI superobject with ``$this->CI =& get_instance()`` and
Derek Jones8ede1a22011-10-05 13:34:52 -0500118 then the finalized data will be available by calling
Andrey Andreev16a704c2012-11-09 17:25:00 +0200119 ``$this->CI->output->get_output()``.
Derek Jones8ede1a22011-10-05 13:34:52 -0500120- **cache_override**
Andrey Andreev16a704c2012-11-09 17:25:00 +0200121 Enables you to call your own method instead of the ``_display_cache()``
122 method in the :doc:`Output Library <../libraries/output>`. This permits
123 you to use your own cache display mechanism.
Derek Jones8ede1a22011-10-05 13:34:52 -0500124- **post_system**
125 Called after the final rendered page is sent to the browser, at the
126 end of system execution after the finalized data is sent to the
Andrey Andreev16a704c2012-11-09 17:25:00 +0200127 browser.