|
| 1 | +#################################### |
| 2 | +Hooks - Extending the Framework Core |
| 3 | +#################################### |
| 4 | + |
| 5 | +CodeIgniter's Hooks feature provides a means to tap into and modify the inner workings of the framework without hacking |
| 6 | +core files. When CodeIgniter runs it follows a specific execution process. There may be instances, however, when you'd |
| 7 | +like to cause some action to take place at a particular stage in the execution process. For example, you might want to run |
| 8 | +a script right before your controllers get loaded, or right after, or you might want to trigger one of your own scripts |
| 9 | +in some other location. |
| 10 | + |
| 11 | +Hooks work on a *publish/subscribe* pattern, where a hook, or event, is triggered at some point during the script execution. |
| 12 | +Other scripts can "subscribe" to that event by registering with the Hooks class to let it know they want to perform an |
| 13 | +action when that hook is triggered. |
| 14 | + |
| 15 | +Enabling Hooks |
| 16 | +============== |
| 17 | + |
| 18 | +Hooks are always enabled, and are available globally. |
| 19 | + |
| 20 | +Defining a Hook |
| 21 | +=============== |
| 22 | + |
| 23 | +Most hooks are defined within the **application/Config/Hooks.php** file. You can subscribe an action to a hook with |
| 24 | +the Hooks class' ``on()`` method. The first parameter is the name of the hook to subscribe to. The second parameter is |
| 25 | +a callable that will be run when that event is triggered.:: |
| 26 | + |
| 27 | + use CodeIgniter\Hooks\Hooks; |
| 28 | + |
| 29 | + Hooks::on('pre_controller', ['MyClass', 'MyFunction']); |
| 30 | + |
| 31 | +In this example, whenever the **pre_controller** hook is executed, an instance of ``MyClass`` is created and the |
| 32 | +``MyFunction`` method is ran. Note that the second parameter can be *any* form of |
| 33 | +`callable <http://php.net/manual/en/function.is-callable.php>`_ that PHP recognizes:: |
| 34 | + |
| 35 | + // Call a standalone function |
| 36 | + Hooks::on('pre_controller', 'some_function'); |
| 37 | + |
| 38 | + // Call on an instance method |
| 39 | + $user = new User(); |
| 40 | + Hooks::on('pre_controller', [$user, 'some_method']); |
| 41 | + |
| 42 | + // Call on a static method |
| 43 | + Hooks::on('pre_controller', 'SomeClass::someMethod'); |
| 44 | + |
| 45 | + // Use a Closure |
| 46 | + Hooks::on('pre_controller', function(...$params) |
| 47 | + { |
| 48 | + . . . |
| 49 | + }); |
| 50 | + |
| 51 | +Setting Priorities |
| 52 | +------------------ |
| 53 | + |
| 54 | +Since multiple methods can be subscribed to a single event, you will need a way to define in what order those methods |
| 55 | +are called. You can do this by passing a priority value as the third parameter of the ``on()`` method. Lower values |
| 56 | +are executed first, with a value of 1 having the highest priority, and there being no limit on the lower values.:: |
| 57 | + |
| 58 | + Hooks::on('pre_controller', 'some_function', 25); |
| 59 | + |
| 60 | +Any subscribers with the same priority will be executed in the order they were defined. |
| 61 | + |
| 62 | +Three constants are defined for your use, that set some helpful ranges on the values. You are not required to use these |
| 63 | +but you might find they aid readability.:: |
| 64 | + |
| 65 | + define('HOOKS_PRIORITY_LOW', 200); |
| 66 | + define('HOOKS_PRIORITY_NORMAL', 100); |
| 67 | + define('HOOKS_PRIORITY_HIGH', 10); |
| 68 | + |
| 69 | +Once sorted, all subscribers are executed in order. If any subscriber returns a boolean false value, then execution of |
| 70 | +the subscribers will stop. |
| 71 | + |
| 72 | +Publishing your own Hooks |
| 73 | +========================= |
| 74 | + |
| 75 | +The Hooks library makes it simple for you to create hooks into your own code, also. To use this feature, you would simply |
| 76 | +need to call the ``trigger()`` method on the **Hooks** class with the name of the hook:: |
| 77 | + |
| 78 | + \CodeIgniter\Hooks\Hooks::trigger('some_hook'); |
| 79 | + |
| 80 | +You can pass any number of arguments to the subscribers by adding them as additional parameters. Subscribers will be |
| 81 | +given the arguments in the same order as defined.:: |
| 82 | + |
| 83 | + \CodeIgniter\Hooks\Hooks::trigger('some_hook', $foo, $bar, $baz); |
| 84 | + |
| 85 | + Hooks::on('some_hook', function($foo, $bar, $baz) { |
| 86 | + ... |
| 87 | + }); |
| 88 | + |
| 89 | +Hook Points |
| 90 | +=========== |
| 91 | + |
| 92 | +The following is a list of available hook points: |
| 93 | + |
| 94 | +* **pre_system** 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. |
| 95 | +* **pre_controller** Called immediately prior to any of your controllers being called. All base classes, routing, and security checks have been done. |
| 96 | +* **post_controller_constructor** Called immediately after your controller is instantiated, but prior to any method calls happening. |
| 97 | +* **post_controller** Called immediately after your controller is fully executed. |
| 98 | +* **post_system** 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. |
0 commit comments