Derek Jones | 8ede1a2 | 2011-10-05 13:34:52 -0500 | [diff] [blame] | 1 | #################################### |
| 2 | Hooks - Extending the Framework Core |
| 3 | #################################### |
| 4 | |
| 5 | CodeIgniter's Hooks feature provides a means to tap into and modify the |
| 6 | inner workings of the framework without hacking the core files. When |
| 7 | CodeIgniter runs it follows a specific execution process, diagramed in |
| 8 | the :doc:`Application Flow <../overview/appflow>` page. There may be |
| 9 | instances, however, where you'd like to cause some action to take place |
| 10 | at a particular stage in the execution process. For example, you might |
| 11 | want to run a script right before your controllers get loaded, or right |
| 12 | after, or you might want to trigger one of your own scripts in some |
| 13 | other location. |
| 14 | |
| 15 | Enabling Hooks |
| 16 | ============== |
| 17 | |
| 18 | The hooks feature can be globally enabled/disabled by setting the |
Andrey Andreev | 16a704c | 2012-11-09 17:25:00 +0200 | [diff] [blame] | 19 | following item in the **application/config/config.php** file:: |
Derek Jones | 8ede1a2 | 2011-10-05 13:34:52 -0500 | [diff] [blame] | 20 | |
| 21 | $config['enable_hooks'] = TRUE; |
| 22 | |
| 23 | Defining a Hook |
| 24 | =============== |
| 25 | |
Andrey Andreev | 8351404 | 2014-03-06 00:28:55 +0200 | [diff] [blame] | 26 | Hooks are defined in the **application/config/hooks.php** file. |
Andrey Andreev | 16a704c | 2012-11-09 17:25:00 +0200 | [diff] [blame] | 27 | Each hook is specified as an array with this prototype:: |
Derek Jones | 8ede1a2 | 2011-10-05 13:34:52 -0500 | [diff] [blame] | 28 | |
Derek Jones | a1360ef | 2011-10-05 17:22:53 -0500 | [diff] [blame] | 29 | $hook['pre_controller'] = array( |
Andrey Andreev | 16a704c | 2012-11-09 17:25:00 +0200 | [diff] [blame] | 30 | 'class' => 'MyClass', |
| 31 | 'function' => 'Myfunction', |
| 32 | 'filename' => 'Myclass.php', |
| 33 | 'filepath' => 'hooks', |
| 34 | 'params' => array('beer', 'wine', 'snacks') |
| 35 | ); |
Derek Jones | 8ede1a2 | 2011-10-05 13:34:52 -0500 | [diff] [blame] | 36 | |
| 37 | **Notes:** |
Andrey Andreev | 16a704c | 2012-11-09 17:25:00 +0200 | [diff] [blame] | 38 | |
Derek Jones | 8ede1a2 | 2011-10-05 13:34:52 -0500 | [diff] [blame] | 39 | The array index correlates to the name of the particular hook point you |
| 40 | want to use. In the above example the hook point is pre_controller. A |
| 41 | list of hook points is found below. The following items should be |
| 42 | defined 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 Andreev | 16a704c | 2012-11-09 17:25:00 +0200 | [diff] [blame] | 46 | - **function** The function (or method) name you wish to call. |
Derek Jones | 8ede1a2 | 2011-10-05 13:34:52 -0500 | [diff] [blame] | 47 | - **filename** The file name containing your class/function. |
Andrey Andreev | 16a704c | 2012-11-09 17:25:00 +0200 | [diff] [blame] | 48 | - **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 Jones | 8ede1a2 | 2011-10-05 13:34:52 -0500 | [diff] [blame] | 55 | filepath. No trailing slash. |
| 56 | - **params** Any parameters you wish to pass to your script. This item |
| 57 | is optional. |
| 58 | |
Andrey Andreev | 8351404 | 2014-03-06 00:28:55 +0200 | [diff] [blame] | 59 | If 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 Jones | 8ede1a2 | 2011-10-05 13:34:52 -0500 | [diff] [blame] | 67 | Multiple Calls to the Same Hook |
| 68 | =============================== |
| 69 | |
Andrey Andreev | ba231aa | 2014-01-20 16:43:41 +0200 | [diff] [blame] | 70 | If want to use the same hook point with more than one script, simply |
Derek Jones | 8ede1a2 | 2011-10-05 13:34:52 -0500 | [diff] [blame] | 71 | make your array declaration multi-dimensional, like this:: |
| 72 | |
Derek Jones | a1360ef | 2011-10-05 17:22:53 -0500 | [diff] [blame] | 73 | $hook['pre_controller'][] = array( |
Andrey Andreev | 16a704c | 2012-11-09 17:25:00 +0200 | [diff] [blame] | 74 | 'class' => 'MyClass', |
| 75 | 'function' => 'MyMethod', |
| 76 | 'filename' => 'Myclass.php', |
| 77 | 'filepath' => 'hooks', |
| 78 | 'params' => array('beer', 'wine', 'snacks') |
| 79 | ); |
Derek Jones | a1360ef | 2011-10-05 17:22:53 -0500 | [diff] [blame] | 80 | |
| 81 | $hook['pre_controller'][] = array( |
Andrey Andreev | 16a704c | 2012-11-09 17:25:00 +0200 | [diff] [blame] | 82 | 'class' => 'MyOtherClass', |
| 83 | 'function' => 'MyOtherMethod', |
| 84 | 'filename' => 'Myotherclass.php', |
| 85 | 'filepath' => 'hooks', |
| 86 | 'params' => array('red', 'yellow', 'blue') |
| 87 | ); |
Derek Jones | 8ede1a2 | 2011-10-05 13:34:52 -0500 | [diff] [blame] | 88 | |
| 89 | Notice the brackets after each array index:: |
| 90 | |
| 91 | $hook['pre_controller'][] |
| 92 | |
| 93 | This permits you to have the same hook point with multiple scripts. The |
| 94 | order you define your array will be the execution order. |
| 95 | |
| 96 | Hook Points |
| 97 | =========== |
| 98 | |
| 99 | The 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 Andreev | 16a704c | 2012-11-09 17:25:00 +0200 | [diff] [blame] | 114 | Overrides the ``_display()`` method, used to send the finalized page |
Derek Jones | 8ede1a2 | 2011-10-05 13:34:52 -0500 | [diff] [blame] | 115 | 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 Andreev | 16a704c | 2012-11-09 17:25:00 +0200 | [diff] [blame] | 117 | reference the CI superobject with ``$this->CI =& get_instance()`` and |
Derek Jones | 8ede1a2 | 2011-10-05 13:34:52 -0500 | [diff] [blame] | 118 | then the finalized data will be available by calling |
Andrey Andreev | 16a704c | 2012-11-09 17:25:00 +0200 | [diff] [blame] | 119 | ``$this->CI->output->get_output()``. |
Derek Jones | 8ede1a2 | 2011-10-05 13:34:52 -0500 | [diff] [blame] | 120 | - **cache_override** |
Andrey Andreev | 16a704c | 2012-11-09 17:25:00 +0200 | [diff] [blame] | 121 | 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 Jones | 8ede1a2 | 2011-10-05 13:34:52 -0500 | [diff] [blame] | 124 | - **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 Andreev | 16a704c | 2012-11-09 17:25:00 +0200 | [diff] [blame] | 127 | browser. |