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 |
|
Derek Allard | 404e35d | 2007-08-07 01:00:45 +0000 | [diff] [blame] | 5 | <title>CodeIgniter User Guide : Hooks</title>
|
admin | b0dd10f | 2006-08-25 17:25:49 +0000 | [diff] [blame] | 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>
|
Derek Allard | b341237 | 2007-10-25 12:15:16 +0000 | [diff] [blame] | 13 | <script type="text/javascript" src="../nav/user_guide_menu.js"></script>
|
admin | b0dd10f | 2006-08-25 17:25:49 +0000 | [diff] [blame] | 14 |
|
| 15 | <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
|
| 16 | <meta http-equiv='expires' content='-1' />
|
| 17 | <meta http-equiv= 'pragma' content='no-cache' />
|
| 18 | <meta name='robots' content='all' />
|
| 19 | <meta name='author' content='Rick Ellis' />
|
Derek Allard | d2df9bc | 2007-04-15 17:41:17 +0000 | [diff] [blame] | 20 | <meta name='description' content='CodeIgniter User Guide' />
|
admin | b0dd10f | 2006-08-25 17:25:49 +0000 | [diff] [blame] | 21 |
|
| 22 | </head>
|
| 23 | <body>
|
| 24 |
|
| 25 | <!-- START NAVIGATION -->
|
| 26 | <div id="nav"><div id="nav_inner"><script type="text/javascript">create_menu('../');</script></div></div>
|
| 27 | <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>
|
| 28 | <div id="masthead">
|
| 29 | <table cellpadding="0" cellspacing="0" border="0" style="width:100%">
|
| 30 | <tr>
|
Derek Allard | 60ca9b7 | 2007-07-12 19:53:27 +0000 | [diff] [blame] | 31 | <td><h1>CodeIgniter User Guide Version 1.5.4</h1></td>
|
admin | c0d5d52 | 2006-10-30 19:40:35 +0000 | [diff] [blame] | 32 | <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] | 33 | </tr>
|
| 34 | </table>
|
| 35 | </div>
|
| 36 | <!-- END NAVIGATION -->
|
| 37 |
|
| 38 |
|
| 39 | <!-- START BREADCRUMB -->
|
| 40 | <table cellpadding="0" cellspacing="0" border="0" style="width:100%">
|
| 41 | <tr>
|
| 42 | <td id="breadcrumb">
|
Derek Allard | d2df9bc | 2007-04-15 17:41:17 +0000 | [diff] [blame] | 43 | <a href="http://www.codeigniter.com/">CodeIgniter Home</a> ›
|
admin | b0dd10f | 2006-08-25 17:25:49 +0000 | [diff] [blame] | 44 | <a href="../index.html">User Guide Home</a> ›
|
| 45 | Hooks - Extending the Framework Core
|
| 46 | </td>
|
Derek Allard | bc03091 | 2007-06-24 18:25:29 +0000 | [diff] [blame] | 47 | <td id="searchbox"><form method="get" action="http://www.google.com/search"><input type="hidden" name="as_sitesearch" id="as_sitesearch" value="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>
|
admin | b0dd10f | 2006-08-25 17:25:49 +0000 | [diff] [blame] | 48 | </tr>
|
| 49 | </table>
|
| 50 | <!-- END BREADCRUMB -->
|
| 51 |
|
| 52 | <br clear="all" />
|
| 53 |
|
| 54 |
|
| 55 | <!-- START CONTENT -->
|
| 56 | <div id="content">
|
| 57 |
|
| 58 | <h1>Hooks - Extending the Framework Core</h1>
|
| 59 |
|
Derek Allard | d2df9bc | 2007-04-15 17:41:17 +0000 | [diff] [blame] | 60 | <p>CodeIgniter's Hooks feature provides a means to tap into and modify the inner workings of the framework without hacking the core files.
|
| 61 | When CodeIgniter runs it follows a specific execution process, diagramed in the <a href="../overview/appflow.html">Application Flow</a> page.
|
admin | e334c47 | 2006-10-21 19:44:22 +0000 | [diff] [blame] | 62 | There may be instances, however, where you'd like to cause some action to take place at a particular stage in the execution process.
|
| 63 | 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] | 64 | your own scripts in some other location.
|
| 65 | </p>
|
| 66 |
|
admin | 2ed76d5 | 2006-09-02 17:34:52 +0000 | [diff] [blame] | 67 | <h2>Enabling Hooks</h2>
|
| 68 |
|
| 69 | <p>The hooks feature can be globally enabled/disabled by setting the following item in the <kbd>application/config/config.php</kbd> file:</p>
|
| 70 |
|
| 71 | <code>$config['enable_hooks'] = TRUE;</code>
|
| 72 |
|
admin | b0dd10f | 2006-08-25 17:25:49 +0000 | [diff] [blame] | 73 |
|
| 74 | <h2>Defining a Hook</h2>
|
| 75 |
|
| 76 | <p>Hooks are defined in <dfn>application/config/hooks.php</dfn> file. Each hook is specified as an array with this prototype:</p>
|
| 77 |
|
| 78 | <code>
|
| 79 | $hook['pre_controller'] = array(<br />
|
| 80 | 'class' => 'MyClass',<br />
|
admin | e334c47 | 2006-10-21 19:44:22 +0000 | [diff] [blame] | 81 | 'function' => 'Myfunction',<br />
|
admin | b0dd10f | 2006-08-25 17:25:49 +0000 | [diff] [blame] | 82 | 'filename' => 'Myclass.php',<br />
|
| 83 | 'filepath' => 'hooks',<br />
|
| 84 | 'params' => array('beer', 'wine', 'snacks')<br />
|
| 85 | );</code>
|
| 86 |
|
admin | e334c47 | 2006-10-21 19:44:22 +0000 | [diff] [blame] | 87 | <p><strong>Notes:</strong><br />The array index correlates to the name of the particular hook point you want to
|
| 88 | 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] | 89 | The following items should be defined in your associative hook array:</p>
|
| 90 |
|
| 91 | <ul>
|
| 92 | <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>
|
| 93 | <li><strong>function</strong> The function name you wish to call.</li>
|
| 94 | <li><strong>filename</strong> The file name containing your class/function.</li>
|
| 95 | <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>
|
| 96 | <li><strong>params</strong> Any parameters you wish to pass to your script. This item is optional.</li>
|
| 97 | </ul>
|
| 98 |
|
| 99 |
|
| 100 | <h2>Multiple Calls to the Same Hook</h2>
|
| 101 |
|
Derek Allard | c644128 | 2007-07-04 23:54:32 +0000 | [diff] [blame] | 102 | <p>If want to use the same hook point with more then one script, simply make your array declaration multi-dimensional, like this:</p>
|
admin | b0dd10f | 2006-08-25 17:25:49 +0000 | [diff] [blame] | 103 |
|
| 104 | <code>
|
| 105 | $hook['pre_controller']<kbd>[]</kbd> = array(<br />
|
| 106 | 'class' => 'MyClass',<br />
|
admin | e334c47 | 2006-10-21 19:44:22 +0000 | [diff] [blame] | 107 | 'function' => 'Myfunction',<br />
|
admin | b0dd10f | 2006-08-25 17:25:49 +0000 | [diff] [blame] | 108 | 'filename' => 'Myclass.php',<br />
|
| 109 | 'filepath' => 'hooks',<br />
|
| 110 | 'params' => array('beer', 'wine', 'snacks')<br />
|
| 111 | );<br />
|
| 112 | <br />
|
| 113 | $hook['pre_controller']<kbd>[]</kbd> = array(<br />
|
| 114 | 'class' => 'MyOtherClass',<br />
|
admin | e334c47 | 2006-10-21 19:44:22 +0000 | [diff] [blame] | 115 | 'function' => 'MyOtherfunction',<br />
|
admin | b0dd10f | 2006-08-25 17:25:49 +0000 | [diff] [blame] | 116 | 'filename' => 'Myotherclass.php',<br />
|
| 117 | 'filepath' => 'hooks',<br />
|
| 118 | 'params' => array('red', 'yellow', 'blue')<br />
|
| 119 | );</code>
|
| 120 |
|
| 121 | <p>Notice the brackets after each array index:</p>
|
| 122 |
|
| 123 | <code>$hook['pre_controller']<kbd>[]</kbd></code>
|
| 124 |
|
| 125 | <p>This permits you to the same hook point with multiple scripts. The order you define your array will be the execution order.</p>
|
| 126 |
|
| 127 |
|
| 128 | <h2>Hook Points</h2>
|
| 129 |
|
| 130 | The following is a list of available hook points.</p>
|
| 131 |
|
| 132 | <ul>
|
| 133 |
|
| 134 | <li><strong>pre_system</strong><br />
|
| 135 | 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>
|
| 136 |
|
| 137 | <li><strong>pre_controller</strong><br />
|
| 138 | Called immediately prior to any of your controllers being called. All base classes, routing, and security checks have been done.</li>
|
| 139 |
|
admin | a16e883 | 2006-09-06 02:48:30 +0000 | [diff] [blame] | 140 | <li><strong>post_controller_constructor</strong><br />
|
admin | 17a890d | 2006-09-27 20:42:42 +0000 | [diff] [blame] | 141 | 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] | 142 |
|
admin | b0dd10f | 2006-08-25 17:25:49 +0000 | [diff] [blame] | 143 | <li><strong>post_controller</strong><br />
|
admin | 17a890d | 2006-09-27 20:42:42 +0000 | [diff] [blame] | 144 | Called immediately after your controller is fully executed.</li>
|
admin | b0dd10f | 2006-08-25 17:25:49 +0000 | [diff] [blame] | 145 |
|
| 146 | <li><strong>display_override</strong><br />
|
admin | e334c47 | 2006-10-21 19:44:22 +0000 | [diff] [blame] | 147 | 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] | 148 | use your own display methodology. Note that the finalized data will be available by calling <dfn>$this->output->get_output()</dfn></li>
|
| 149 |
|
| 150 | <li><strong>cache_override</strong><br />
|
| 151 | 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>
|
| 152 |
|
| 153 | <li><strong>scaffolding_override</strong><br />
|
| 154 | Permits a scaffolding request to trigger your own script instead.</li>
|
| 155 |
|
| 156 | <li><strong>post_system</strong><br />
|
| 157 | 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>
|
| 158 |
|
| 159 |
|
| 160 | </ul>
|
| 161 |
|
| 162 |
|
| 163 |
|
| 164 |
|
| 165 |
|
| 166 |
|
| 167 |
|
| 168 |
|
| 169 |
|
| 170 |
|
| 171 |
|
| 172 | </div>
|
| 173 | <!-- END CONTENT -->
|
| 174 |
|
| 175 |
|
| 176 | <div id="footer">
|
| 177 | <p>
|
admin | a634e56 | 2006-08-25 22:19:55 +0000 | [diff] [blame] | 178 | Previous Topic: <a href="core_classes.html">Creating Core Classes</a>
|
admin | b0dd10f | 2006-08-25 17:25:49 +0000 | [diff] [blame] | 179 | ·
|
| 180 | <a href="#top">Top of Page</a> ·
|
| 181 | <a href="../index.html">User Guide Home</a> ·
|
admin | a634e56 | 2006-08-25 22:19:55 +0000 | [diff] [blame] | 182 | Next Topic: <a href="autoloader.html">Auto-loading Resources</a>
|
Derek Allard | c644128 | 2007-07-04 23:54:32 +0000 | [diff] [blame] | 183 | </p>
|
Derek Allard | d2df9bc | 2007-04-15 17:41:17 +0000 | [diff] [blame] | 184 | <p><a href="http://www.codeigniter.com">CodeIgniter</a> · Copyright © 2007 · <a href="http://ellislab.com/">Ellislab, Inc.</a></p>
|
admin | b0dd10f | 2006-08-25 17:25:49 +0000 | [diff] [blame] | 185 | </div>
|
| 186 |
|
| 187 | </body>
|
| 188 | </html> |