In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-04-13 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/03 Report--
This article mainly introduces how to use the CodeIgniter hook, has a certain reference value, interested friends can refer to, I hope you can learn a lot after reading this article, the following let the editor take you to understand it.
The details are as follows:
There are seven oval boxes in the CodeIgniter execution flowchart, which indicates that seven hooks are buried during execution. Let's take a look at what a hook is.
Two paragraphs found on the Internet:
The complete implementation of the hook should be called event-driven. The event driver is divided into two stages. the first stage is to register the event, which aims to give a name to the "event" that may occur in the future. the simple implementation method is to use the singleton pattern to generate a persistent object or register a global variable. then insert the event name and the corresponding classes and methods into the global variable. That is, to mount a hook.
The second stage is to trigger the event, which is essentially to query the name of the event to be triggered in the global variable of the event, and then find the registered classes and methods, instantiate and run. In this way, we can get rid of the traditional rule that the program must be in order, and further achieve the purpose of decoupling.
Hook functions can intercept and process messages from other applications. Whenever a specific message is sent, the hook program captures the message before it reaches the destination window, that is, the hook function gets control first. At this point, the hook function can either process (change) the message, or continue to deliver the message without processing, or force the delivery of the message to end.
You can see:
1. The hook function is preset and triggered under specific conditions.
2. After the hook function takes over the program, it can affect the direction of the program.
CI preset hook
Seven hooks are set in CI, and the hooks are relatively independent of each other. The first three hooks are set before instantiation, meaning that get_instance instances cannot be used, and global is required to use instantiated classes. The function of the 7 hooks is clearly stated in the manual, such as rewriting the cache output, processing the output, and so on.
How do I trigger a CI hook?
CI sets the hook switch in the configuration file. To use the hook, you must first turn on 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'))
When the controller is instantiated, the post_controller_constructor method in the Hook.php in the hooks directory is triggered before the action is called. We can do some processing in this method.
The Application of Hook
So far, I have not realized the beauty of the CI hook. Check that there are some programs on the Internet that use the CI hook to achieve authority control, that is, to determine the authority in post_controller_constructor. Because this hook is like a constructor, it is feasible to judge whether the method to be executed next has permission in the constructor. But this function can also be implemented in MY_Controller, or even better. Because the hook of CI is global, it will be started both in the foreground and in the background. Although this hook program can make a judgment, it is not necessarily the best one here. As mentioned earlier, the common controllers of different modules in MY_Controller are separated, and the foreground and background inherit different controllers. If I only need to control the permissions in the background, it can be realized directly in the public controller in the background, without affecting the foreground and only controlling the parts that are needed.
Here is an example that is not in CI, where the thinking is the same. Cooperation channel user login registration function, there are 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 when we encounter problems, we can look at them in our daily life. Take the registration processing logic as an example, you can see that one of the processes is unchanged. Accept parameters-> process before registration-> register-> process after registration. What has changed? Each step may change, but the process remains the same. Therefore, you can set hooks for front, back, processing and other related places, and distribute the processing logic to specific situations. Here is a reference diagram. Specific hooks need to be set according to the project:
Facts have also proved that this approach is feasible, although there are many partners, but can also be grouped, hundreds of registration pages can be easily implemented. So it is convenient to use hooks to deal with the same process and the ever-changing requirements of a step in the middle.
To put it simply, a hook is to execute a program under certain conditions; to put it more simply, a hook is a way to undo if judgment.
Too much if judgment can make the program difficult to read and maintain, and the handling of hooks can make the program more flexible. Hooks have certain trigger conditions, which can be configured, read from the database, or implemented by some techniques, such as reflection, etc., using hooks can achieve the purpose of decoupling.
Thank you for reading this article carefully. I hope the article "how to use CodeIgniter Hook" shared by the editor will be helpful to everyone. At the same time, I also hope you will support us and pay attention to the industry information channel. More related knowledge is waiting for you to learn!
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.