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 |
| 19 | following item in the application/config/config.php file:: |
| 20 | |
| 21 | $config['enable_hooks'] = TRUE; |
| 22 | |
| 23 | Defining a Hook |
| 24 | =============== |
| 25 | |
| 26 | Hooks are defined in application/config/hooks.php file. Each hook is |
| 27 | specified 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:** |
| 32 | The array index correlates to the name of the particular hook point you |
| 33 | want to use. In the above example the hook point is pre_controller. A |
| 34 | list of hook points is found below. The following items should be |
| 35 | defined 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 | |
| 51 | Multiple Calls to the Same Hook |
| 52 | =============================== |
| 53 | |
| 54 | If want to use the same hook point with more then one script, simply |
| 55 | make 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 | |
| 59 | Notice the brackets after each array index:: |
| 60 | |
| 61 | $hook['pre_controller'][] |
| 62 | |
| 63 | This permits you to have the same hook point with multiple scripts. The |
| 64 | order you define your array will be the execution order. |
| 65 | |
| 66 | Hook Points |
| 67 | =========== |
| 68 | |
| 69 | The 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 | |