This article mainly introduces the usage of CodeIgniter hooks, analyzes and illustrates the concept of hooks in CodeIgniter, and demonstrates the use of hooks in the form of examples, for more information about CodeIgniter hook usage, see the example in this article. We will share this with you for your reference. The details are as follows:
The execution flowchart of CodeIgniter contains seven elliptical boxes, which indicates that seven hooks are embedded during execution. What is a hook?
Two texts found on the Internet:
The complete implementation of hooks should be called event-driven. Event-driven systems are divided into two phases. The first phase is to register an event to name a possible "Event" in the future, A simple implementation method is to generate a Persistent Object in singleton mode or register a global variable, and then insert the event name and the class and method corresponding to the event into the global variable. That is, mount a hook.
The second stage is the trigger event. In essence, it is to query the name of the event to be triggered in the global variables of the event, find the registered class and method, instantiate and run the event. In this way, you can get rid of the rules that the program must follow in order in the traditional mode to further remove coupling.
The hook function can intercept and process messages from other applications. When a specific message is sent, the hook program captures the message before it reaches the target window, that is, the hook function gets control of the message first. In this case, the hook function can process (Change) the message, continue to transmit the message without processing it, and forcibly end the message transmission.
You can see:
1. Hook Functions are preset and triggered under specific conditions.
2. After the hook function takes over the program, it can affect the program trend.
CI preset hook
Seven hooks are set in CI, and they are relatively independent from each other. The first three hooks are set before instantiation, meaning that the get_instance instance cannot be used. To use an instantiated class, global is required. The seven hooks are clearly described in the reference manual. You can rewrite the cache output and process the output.
How to trigger a CI hook?
CI sets the hook switch in the configuration file. To use the hook, you must first enable the switch, and then configure the hook array in config/hook. php, such as setting the post_controller_constructor hook:
$hook['post_controller_constructor'] = array( 'class' => 'Hook', 'function' => 'post_controller_constructor', 'filename' => 'Hook.php', 'filepath' => 'hooks', 'params' => array('beer', 'wine', 'snacks'));
After the controller is instantiated, The post_controller_constructor method in Hook. php under the hooks directory will be triggered before the action is called. We can do some processing in this method.
Hook Application
So far, I have not realized the excellent CI hooks. Check that there are some apps on the Internet that use CI hooks to implement permission control, that is, permission judgment on post_controller_constructor, because this hook is like a constructor, it is feasible to judge whether the method to be executed is permitted in the constructor. However, this function can be fully implemented in MY_Controller, or even better. Because the CI hook is global, the hook will be started on both the foreground and background. Although this hook program can be used for judgment, it is not necessarily the best here. As mentioned above, the public controllers of different modules in MY_Controller are separated, and the front-end backend inherits different controllers. If I only need to control the permissions of the backend, it can be directly implemented in the backend public controllers, it does not affect the front-end and only controls the required parts.
Here is an example of non-CI, with the same thinking. The user login and registration function of the partner channel has many partners. Different partners need to register the same basic information, but each partner may have some special fields or different verification methods.
Many times we encounter problems that can be viewed in our daily lives. Taking the registration processing logic as an example, we can see that one of the processes remains unchanged. Accept parameters-> process before registration-> Register-> process after registration. What is the change? Each step may change, but the process does not change. Therefore, you can set hooks for pre-, post-, and processing to distribute the processing logic to specific situations. The following is a reference image, the specific setting of hooks also depends on the project:
Facts also prove that this method is feasible. Although there are many partners, it can also be grouped, and hundreds of registration pages can be easily implemented. Therefore, it is convenient to use hooks to process such procedures without changing the number of steps in the middle.
To put it simply, a hook is to execute a program under a specific condition. To put it simply, a hook is a way to cancel the if judgment.
Too much if judgment will make the program difficult to read and maintain, and the hook processing can make the program more flexible. Hooks have certain trigger conditions, which can be configured, read from the database, or implemented through some technologies, such as reflection. Hooks can be used for decoupling.