admin | b0dd10f | 2006-08-25 17:25:49 +0000 | [diff] [blame] | 1 | <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
|
| 2 | <html>
|
| 3 | <head>
|
| 4 |
|
| 5 | <title>Code Igniter User Guide</title>
|
| 6 |
|
| 7 | <style type='text/css' media='all'>@import url('../userguide.css');</style>
|
| 8 | <link rel='stylesheet' type='text/css' media='all' href='../userguide.css' />
|
| 9 |
|
admin | 17a890d | 2006-09-27 20:42:42 +0000 | [diff] [blame] | 10 | <script type="text/javascript" src="../nav/nav.js"></script>
|
admin | 2296fc3 | 2006-09-27 21:07:02 +0000 | [diff] [blame] | 11 | <script type="text/javascript" src="../nav/prototype.lite.js"></script>
|
admin | 17a890d | 2006-09-27 20:42:42 +0000 | [diff] [blame] | 12 | <script type="text/javascript" src="../nav/moo.fx.js"></script>
|
admin | b0dd10f | 2006-08-25 17:25:49 +0000 | [diff] [blame] | 13 | <script type="text/javascript">
|
| 14 | window.onload = function() {
|
admin | e334c47 | 2006-10-21 19:44:22 +0000 | [diff] [blame] | 15 | myHeight = new fx.Height('nav', {duration: 400});
|
admin | b0dd10f | 2006-08-25 17:25:49 +0000 | [diff] [blame] | 16 | myHeight.hide();
|
| 17 | }
|
| 18 | </script>
|
| 19 |
|
| 20 | <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
|
| 21 | <meta http-equiv='expires' content='-1' />
|
| 22 | <meta http-equiv= 'pragma' content='no-cache' />
|
| 23 | <meta name='robots' content='all' />
|
| 24 | <meta name='author' content='Rick Ellis' />
|
| 25 | <meta name='description' content='Code Igniter User Guide' />
|
| 26 |
|
| 27 | </head>
|
| 28 | <body>
|
| 29 |
|
| 30 | <!-- START NAVIGATION -->
|
| 31 | <div id="nav"><div id="nav_inner"><script type="text/javascript">create_menu('../');</script></div></div>
|
| 32 | <div id="nav2"><a name="top"></a><a href="javascript:void(0);" onclick="myHeight.toggle();"><img src="../images/nav_toggle.jpg" width="153" height="44" border="0" title="Toggle Table of Contents" alt="Toggle Table of Contents" /></a></div>
|
| 33 | <div id="masthead">
|
| 34 | <table cellpadding="0" cellspacing="0" border="0" style="width:100%">
|
| 35 | <tr>
|
admin | 0518661 | 2006-10-28 03:28:01 +0000 | [diff] [blame] | 36 | <td><h1>Code Igniter User Guide Version 1.5.0</h1></td>
|
admin | c0d5d52 | 2006-10-30 19:40:35 +0000 | [diff] [blame] | 37 | <td id="breadcrumb_right"><a href="../toc.html">Table of Contents Page</a></td>
|
admin | b0dd10f | 2006-08-25 17:25:49 +0000 | [diff] [blame] | 38 | </tr>
|
| 39 | </table>
|
| 40 | </div>
|
| 41 | <!-- END NAVIGATION -->
|
| 42 |
|
| 43 |
|
| 44 | <!-- START BREADCRUMB -->
|
| 45 | <table cellpadding="0" cellspacing="0" border="0" style="width:100%">
|
| 46 | <tr>
|
| 47 | <td id="breadcrumb">
|
| 48 | <a href="http://www.codeigniter.com/">Code Igniter Home</a> ›
|
| 49 | <a href="../index.html">User Guide Home</a> ›
|
| 50 | Hooks - Extending the Framework Core
|
| 51 | </td>
|
| 52 | <td id="searchbox"><form method="get" action="http://www.google.com/search"><input type="hidden" name="as_sitesearch" id="as_sitesearch" value="www.codeigniter.com/user_guide/" />Search User Guide <input type="text" class="input" style="width:200px;" name="q" id="q" size="31" maxlength="255" value="" /> <input type="submit" class="submit" name="sa" value="Go" /></form></td>
|
| 53 | </tr>
|
| 54 | </table>
|
| 55 | <!-- END BREADCRUMB -->
|
| 56 |
|
| 57 | <br clear="all" />
|
| 58 |
|
| 59 |
|
| 60 | <!-- START CONTENT -->
|
| 61 | <div id="content">
|
| 62 |
|
| 63 | <h1>Hooks - Extending the Framework Core</h1>
|
| 64 |
|
admin | e334c47 | 2006-10-21 19:44:22 +0000 | [diff] [blame] | 65 | <p>Code Igniter's Hooks feature provides a means to tap into and modify the inner workings of the framework without hacking the core files.
|
| 66 | When Code Igniter runs it follows a specific execution process, diagramed in the <a href="../overview/appflow.html">Application Flow</a> page.
|
| 67 | There may be instances, however, where you'd like to cause some action to take place at a particular stage in the execution process.
|
| 68 | For example, you might want to run a script right before your controllers get loaded, or right after, or you might want to trigger one of
|
admin | b0dd10f | 2006-08-25 17:25:49 +0000 | [diff] [blame] | 69 | your own scripts in some other location.
|
| 70 | </p>
|
| 71 |
|
admin | 2ed76d5 | 2006-09-02 17:34:52 +0000 | [diff] [blame] | 72 | <h2>Enabling Hooks</h2>
|
| 73 |
|
| 74 | <p>The hooks feature can be globally enabled/disabled by setting the following item in the <kbd>application/config/config.php</kbd> file:</p>
|
| 75 |
|
| 76 | <code>$config['enable_hooks'] = TRUE;</code>
|
| 77 |
|
admin | b0dd10f | 2006-08-25 17:25:49 +0000 | [diff] [blame] | 78 |
|
| 79 | <h2>Defining a Hook</h2>
|
| 80 |
|
| 81 | <p>Hooks are defined in <dfn>application/config/hooks.php</dfn> file. Each hook is specified as an array with this prototype:</p>
|
| 82 |
|
| 83 | <code>
|
| 84 | $hook['pre_controller'] = array(<br />
|
| 85 | 'class' => 'MyClass',<br />
|
admin | e334c47 | 2006-10-21 19:44:22 +0000 | [diff] [blame] | 86 | 'function' => 'Myfunction',<br />
|
admin | b0dd10f | 2006-08-25 17:25:49 +0000 | [diff] [blame] | 87 | 'filename' => 'Myclass.php',<br />
|
| 88 | 'filepath' => 'hooks',<br />
|
| 89 | 'params' => array('beer', 'wine', 'snacks')<br />
|
| 90 | );</code>
|
| 91 |
|
admin | e334c47 | 2006-10-21 19:44:22 +0000 | [diff] [blame] | 92 | <p><strong>Notes:</strong><br />The array index correlates to the name of the particular hook point you want to
|
| 93 | use. In the above example the hook point is <kbd>pre_controller</kbd>. A list of hook points is found below.
|
admin | b0dd10f | 2006-08-25 17:25:49 +0000 | [diff] [blame] | 94 | The following items should be defined in your associative hook array:</p>
|
| 95 |
|
| 96 | <ul>
|
| 97 | <li><strong>class</strong> The name of the class you wish to invoke. If you prefer to use a procedural function instead of a class, leave this item blank.</li>
|
| 98 | <li><strong>function</strong> The function name you wish to call.</li>
|
| 99 | <li><strong>filename</strong> The file name containing your class/function.</li>
|
| 100 | <li><strong>filepath</strong> The name of the directory containing your script. Note: Your script must be located in a directory INSIDE your <kbd>application</kbd> folder, so the file path is relative to that folder. For example, if your script is located in <dfn>application/hooks</dfn>, you will simply use <samp>hooks</samp> as your filepath. If your script is located in <dfn>application/hooks/utilities</dfn> you will use <samp>hooks/utilities</samp> as your filepath. No trailing slash.</li>
|
| 101 | <li><strong>params</strong> Any parameters you wish to pass to your script. This item is optional.</li>
|
| 102 | </ul>
|
| 103 |
|
| 104 |
|
| 105 | <h2>Multiple Calls to the Same Hook</h2>
|
| 106 |
|
| 107 | <p>If want to use the same hook point with more then one script, simply make your array declaration multi-dimensional, like this:
|
| 108 |
|
| 109 | <code>
|
| 110 | $hook['pre_controller']<kbd>[]</kbd> = array(<br />
|
| 111 | 'class' => 'MyClass',<br />
|
admin | e334c47 | 2006-10-21 19:44:22 +0000 | [diff] [blame] | 112 | 'function' => 'Myfunction',<br />
|
admin | b0dd10f | 2006-08-25 17:25:49 +0000 | [diff] [blame] | 113 | 'filename' => 'Myclass.php',<br />
|
| 114 | 'filepath' => 'hooks',<br />
|
| 115 | 'params' => array('beer', 'wine', 'snacks')<br />
|
| 116 | );<br />
|
| 117 | <br />
|
| 118 | $hook['pre_controller']<kbd>[]</kbd> = array(<br />
|
| 119 | 'class' => 'MyOtherClass',<br />
|
admin | e334c47 | 2006-10-21 19:44:22 +0000 | [diff] [blame] | 120 | 'function' => 'MyOtherfunction',<br />
|
admin | b0dd10f | 2006-08-25 17:25:49 +0000 | [diff] [blame] | 121 | 'filename' => 'Myotherclass.php',<br />
|
| 122 | 'filepath' => 'hooks',<br />
|
| 123 | 'params' => array('red', 'yellow', 'blue')<br />
|
| 124 | );</code>
|
| 125 |
|
| 126 | <p>Notice the brackets after each array index:</p>
|
| 127 |
|
| 128 | <code>$hook['pre_controller']<kbd>[]</kbd></code>
|
| 129 |
|
| 130 | <p>This permits you to the same hook point with multiple scripts. The order you define your array will be the execution order.</p>
|
| 131 |
|
| 132 |
|
| 133 | <h2>Hook Points</h2>
|
| 134 |
|
| 135 | The following is a list of available hook points.</p>
|
| 136 |
|
| 137 | <ul>
|
| 138 |
|
| 139 | <li><strong>pre_system</strong><br />
|
| 140 | Called very early during system execution. Only the benchmark and hooks class have been loaded at this point. No routing or other processes have happened.</li>
|
| 141 |
|
| 142 | <li><strong>pre_controller</strong><br />
|
| 143 | Called immediately prior to any of your controllers being called. All base classes, routing, and security checks have been done.</li>
|
| 144 |
|
admin | a16e883 | 2006-09-06 02:48:30 +0000 | [diff] [blame] | 145 | <li><strong>post_controller_constructor</strong><br />
|
admin | 17a890d | 2006-09-27 20:42:42 +0000 | [diff] [blame] | 146 | Called immediately after your controller is instantiated, but prior to any method calls happening.</li>
|
admin | a16e883 | 2006-09-06 02:48:30 +0000 | [diff] [blame] | 147 |
|
admin | b0dd10f | 2006-08-25 17:25:49 +0000 | [diff] [blame] | 148 | <li><strong>post_controller</strong><br />
|
admin | 17a890d | 2006-09-27 20:42:42 +0000 | [diff] [blame] | 149 | Called immediately after your controller is fully executed.</li>
|
admin | b0dd10f | 2006-08-25 17:25:49 +0000 | [diff] [blame] | 150 |
|
| 151 | <li><strong>display_override</strong><br />
|
admin | e334c47 | 2006-10-21 19:44:22 +0000 | [diff] [blame] | 152 | Overrides the <dfn>_display()</dfn> function, used to send the finalized page to the web browser at the end of system execution. This permits you to
|
admin | b0dd10f | 2006-08-25 17:25:49 +0000 | [diff] [blame] | 153 | use your own display methodology. Note that the finalized data will be available by calling <dfn>$this->output->get_output()</dfn></li>
|
| 154 |
|
| 155 | <li><strong>cache_override</strong><br />
|
| 156 | Enables you to call your own function instead of the <dfn>_display_cache()</dfn> function in the output class. This permits you to use your own cache display mechanism.</li>
|
| 157 |
|
| 158 | <li><strong>scaffolding_override</strong><br />
|
| 159 | Permits a scaffolding request to trigger your own script instead.</li>
|
| 160 |
|
| 161 | <li><strong>post_system</strong><br />
|
| 162 | Called after the final rendered page is sent to the browser, at the end of system execution after the finalized data is sent to the browser.</li>
|
| 163 |
|
| 164 |
|
| 165 | </ul>
|
| 166 |
|
| 167 |
|
| 168 |
|
| 169 |
|
| 170 |
|
| 171 |
|
| 172 |
|
| 173 |
|
| 174 |
|
| 175 |
|
| 176 |
|
| 177 | </div>
|
| 178 | <!-- END CONTENT -->
|
| 179 |
|
| 180 |
|
| 181 | <div id="footer">
|
| 182 | <p>
|
admin | a634e56 | 2006-08-25 22:19:55 +0000 | [diff] [blame] | 183 | Previous Topic: <a href="core_classes.html">Creating Core Classes</a>
|
admin | b0dd10f | 2006-08-25 17:25:49 +0000 | [diff] [blame] | 184 | ·
|
| 185 | <a href="#top">Top of Page</a> ·
|
| 186 | <a href="../index.html">User Guide Home</a> ·
|
admin | a634e56 | 2006-08-25 22:19:55 +0000 | [diff] [blame] | 187 | Next Topic: <a href="autoloader.html">Auto-loading Resources</a>
|
admin | b0dd10f | 2006-08-25 17:25:49 +0000 | [diff] [blame] | 188 | <p>
|
| 189 | <p><a href="http://www.codeigniter.com">Code Igniter</a> · Copyright © 2006 · <a href="http://www.pmachine.com">pMachine, Inc.</a></p>
|
| 190 | </div>
|
| 191 |
|
| 192 | </body>
|
| 193 | </html> |