CodeIgniter Hook Usage Example detailed, CodeIgniter hook _php Tutorial

Source: Internet
Author: User
Tags codeigniter


CodeIgniter Hook Usage Example detailed, CodeIgniter hook




Examples of this article describe the use of codeigniter hooks. Share to everyone for your reference, as follows:



The CodeIgniter executes a box with 7 ellipses in the process diagram, indicating that 7 hooks were buried during execution. Well, let's see what hooks are.



Two paragraphs of text found online:



The complete implementation of the hooks should be called event-driven. The event driver is divided into two stages, the first stage is the registration event, the purpose is to give a name for future "events", the simple implementation is to use a singleton pattern to produce a persistent object or register a global variable, and then the event name, and the event corresponding class and method to insert global variables. That is, to mount a hook.



The second stage is the trigger event, which essentially queries the event name to be triggered in the global variables of the event, and then finds the registered class and method, instantiates and runs. This way you can get rid of the traditional way the program must follow the rules in order to further achieve the purpose of decoupling.



The hook function can intercept and process messages from other applications. Whenever a particular message is issued, the hook program captures the message before it reaches the destination window, i.e. the hook function gets control first. At this point the hook function can process (change) the message, or you can continue to pass the message without processing, and you can force the end of the message delivery.



can see:



1, the hook function is preset and triggered under certain conditions.



2, hook function takeover program can affect the direction of the program.



CI Preset hooks





7 hooks are set in CI, and the hooks and hooks are relatively independent. The first 3 hooks are set before instantiation, meaning that get_instance instances cannot be used, and global is required to use classes that have already been instantiated. 7 hooks in the function manual is more clear, you can rewrite the cache output, output can be processed 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 config/hook.php in the hook array, such as set 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, it is triggered to execute the Post_controller_constructor method in hook.php in the Hooks directory before the action is invoked. We can do some processing in this method.



Application of Hooks


So far have not realized the beauty of CI hooks, see online There are some application of the CI hooks to implement the rights control program, that is, in the Post_controller_constructor do permission to judge, because this hook is like a constructor, In the constructor, it is feasible to judge whether the method to be executed next has permission. But this function can also be put in my_controller to achieve, even better. Because the CI hook is global, whether it is the front desk or the background will start the hook, although said the hook procedure can be judged, but here is not necessarily the best judgment. Mentioned earlier My_controller in different modules public controller separate, foreground background inherit different controller, if I only need to control the background permissions, can be directly in the background of the public controller to achieve, does not affect the foreground, only the part of the need to control.



Here is a non-ci example, thinking is the same. Cooperative channel of the 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 and we can see them in our daily life. Take the registration processing logic as an example to see if there is a process that is unchanged. Accept the registration information---registration and registration, before processing. What is the change? Each step can change, but the process does not change. So it is possible to set hooks on the front, post, processing and other related places, to distribute the logic of processing to a specific situation, the following is a reference map, specific how to set the hook also need to according to the project:






The fact also proves that this way is feasible, although many partners, but also can be grouped, hundreds of registration pages are easy to achieve. So the use of hooks to deal with this process is constant, and a step in the middle of a variable demand is very convenient.



Simply put, the hook is the execution of a program under certain conditions, and simply, the hook is a way to achieve the cancellation of the If judgment.


Too many if judgments can cause the program to be difficult to read and maintain, and the processing of the hooks makes the program more flexible. Hooks have certain trigger conditions, which can be configured, read from the database, or implemented through some techniques, such as reflection, which can be used for decoupling purposes.



More interested in CodeIgniter related content readers can view this site topic: "CodeIgniter Introductory Tutorial" and "CI (codeigniter) Framework Advanced Tutorial"



It is hoped that this article is helpful to the PHP program design based on CodeIgniter framework.



Articles you may be interested in:


    • CodeIgniter Custom Controller My_controller usage Analysis
    • Case analysis of controller succession problem in CodeIgniter controllers
    • 2 codeigniter file Batch Upload Controller example
    • Example analysis of database.php usage of codeigniter configuration
    • CodeIgniter Multi-language Implementation method detailed
    • CI (CodeIgniter) Model Usage Example analysis
    • CodeIgniter View Usage Considerations
    • CodeIgniter Read and write separation implementation method detailed
    • CI (CodeIgniter) Simple statistics access to the number of implementation methods
    • Analysis of the business logic example of CodeIgniter controller





http://www.bkjia.com/PHPjc/1094762.html www.bkjia.com true http://www.bkjia.com/PHPjc/1094762.html techarticle CodeIgniter Hook Usage example detailed, CodeIgniter hook This example describes the CodeIgniter hook usage. Share to everyone for your reference, as follows: CodeIgniter execution process diagram has ...

Related Article

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.