CodeIgniter Hook Usage Example detailed _php example

Source: Internet
Author: User
Tags codeigniter

This example describes the use of CodeIgniter hooks. Share to everyone for your reference, specific as follows:

CodeIgniter executes a box with 7 ellipses in the process diagram, which indicates that 7 hooks were buried during execution. So let's see what the hook is.

Two pieces of text found online:

The full implementation of the hook should be called event-driven. Event-driven is divided into two phases, the first phase is the registration event, which is intended to give a name to future events that may occur, simply by generating a persistent object in a single case pattern or by registering a global variable, and then inserting the event name and the class and method corresponding to the event into the global variable. Which is to mount a hook.

The second phase is the triggering event, which essentially queries the event name to be triggered in the global variable of the events, then finds the registered class and method, instantiates and runs. This way you can get rid of the traditional way the program must be in order to further achieve the goal of decoupling.

The hook function can intercept and process messages from other applications. Whenever a particular message is sent, the hook program captures the message before it reaches the destination window, which means that the hook function gets control first. The hook function can then process (change) the message, or it can continue to deliver the message without processing, and can force the end of the message to be passed.

You can see:

1. The hook function is a preset and is triggered under certain conditions.

2, hook function to take over the program can affect the direction of the program.

CI Preset hooks

7 hooks are set in CI, and the hooks are relatively independent from the hooks. The first 3 hooks were set prior to instantiation, meaning that the get_instance instance cannot be used, and global is required to use the instantiated class. 7 hooks in the role of the manual said more clearly, you can rewrite the cache output, you can handle the output and so on.

How do I trigger a CI hook?

CI set the hook switch in the configuration file to use the hook first to open the switch, and then configure the hook array in the 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 call to action triggers the execution of the Post_controller_constructor method in the hook.php in the hooks directory. We can do some processing in this method.

Application of Hooks

So far has not realized the wonderful point of CI hooks, look at some of the online application of CI hooks to achieve the right to control the program, that is, in Post_controller_constructor do permission to judge, because this hook is like a constructor, It is feasible to determine in the constructor whether the next method to be executed has permission. But this function can also be put in the my_controller to achieve, even better. Because CI hooks are global, whether the front or backstage will start the hook, although this hook program can make judgments, but here is not necessarily the best judge. Mentioned in the previous My_controller in different modules of the public controller, the foreground background to inherit different controllers, if I only need to control the background permissions, can be directly in the background of the public controller to achieve, do not affect the front desk, only the need to control the part.

Here is an example of a non ci in which the mind 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 calibration methods.

Most of the time we have problems we can put into our daily life to see. Take the registration processing logic as an example to see if one of the processes is unchanged. Accept parameter-> registration data before processing-> registration-> registration data. What is the change? Each step can change, but the process does not change. So can be before, after, processing and other related places to set hooks, the processing of the logic distribution to a specific situation, the following has a reference map, specific how to set the hook also need to be based on the project:

The fact has proved that this approach is feasible, although many partners, but also can be grouped, hundreds of registration page is easy to achieve. Therefore, the use of hooks to deal with this process is unchanged, and the middle of a certain step of the variable demand is very convenient.

In simple terms, a hook is a program that executes under certain conditions, and, simply put, a hook is a way to implement a release if judgment.

Too many if judgments can cause programs to be difficult to read and maintain, and processing with hooks makes the program more flexible. Hooks have certain trigger conditions, the conditions can be configured, read from the database, or through some technology to achieve, such as reflection, using hooks can achieve decoupling purposes.

More readers interested in CodeIgniter-related content can view the site topics: "CodeIgniter Introductory Course" and "CI (CodeIgniter) Framework Advanced Course"

I hope this article will help you with the PHP program design based on CodeIgniter framework.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.