blob: fc5da5b80dd8955a2a6d76488df5ea031d28d4d4 [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 Andreev16a704c2012-11-09 17:25:00 +020026Hooks are defined in **application/config/hooks.php** file.
27Each 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
59Multiple Calls to the Same Hook
60===============================
61
62If want to use the same hook point with more then one script, simply
63make your array declaration multi-dimensional, like this::
64
Derek Jonesa1360ef2011-10-05 17:22:53 -050065 $hook['pre_controller'][] = array(
Andrey Andreev16a704c2012-11-09 17:25:00 +020066 'class' => 'MyClass',
67 'function' => 'MyMethod',
68 'filename' => 'Myclass.php',
69 'filepath' => 'hooks',
70 'params' => array('beer', 'wine', 'snacks')
71 );
Derek Jonesa1360ef2011-10-05 17:22:53 -050072
73 $hook['pre_controller'][] = array(
Andrey Andreev16a704c2012-11-09 17:25:00 +020074 'class' => 'MyOtherClass',
75 'function' => 'MyOtherMethod',
76 'filename' => 'Myotherclass.php',
77 'filepath' => 'hooks',
78 'params' => array('red', 'yellow', 'blue')
79 );
Derek Jones8ede1a22011-10-05 13:34:52 -050080
81Notice the brackets after each array index::
82
83 $hook['pre_controller'][]
84
85This permits you to have the same hook point with multiple scripts. The
86order you define your array will be the execution order.
87
88Hook Points
89===========
90
91The following is a list of available hook points.
92
93- **pre_system**
94 Called very early during system execution. Only the benchmark and
95 hooks class have been loaded at this point. No routing or other
96 processes have happened.
97- **pre_controller**
98 Called immediately prior to any of your controllers being called.
99 All base classes, routing, and security checks have been done.
100- **post_controller_constructor**
101 Called immediately after your controller is instantiated, but prior
102 to any method calls happening.
103- **post_controller**
104 Called immediately after your controller is fully executed.
105- **display_override**
Andrey Andreev16a704c2012-11-09 17:25:00 +0200106 Overrides the ``_display()`` method, used to send the finalized page
Derek Jones8ede1a22011-10-05 13:34:52 -0500107 to the web browser at the end of system execution. This permits you
108 to use your own display methodology. Note that you will need to
Andrey Andreev16a704c2012-11-09 17:25:00 +0200109 reference the CI superobject with ``$this->CI =& get_instance()`` and
Derek Jones8ede1a22011-10-05 13:34:52 -0500110 then the finalized data will be available by calling
Andrey Andreev16a704c2012-11-09 17:25:00 +0200111 ``$this->CI->output->get_output()``.
Derek Jones8ede1a22011-10-05 13:34:52 -0500112- **cache_override**
Andrey Andreev16a704c2012-11-09 17:25:00 +0200113 Enables you to call your own method instead of the ``_display_cache()``
114 method in the :doc:`Output Library <../libraries/output>`. This permits
115 you to use your own cache display mechanism.
Derek Jones8ede1a22011-10-05 13:34:52 -0500116- **post_system**
117 Called after the final rendered page is sent to the browser, at the
118 end of system execution after the finalized data is sent to the
Andrey Andreev16a704c2012-11-09 17:25:00 +0200119 browser.