YAF 0 Basic Learning Summary 7-Learn to use plugins in YAF

Source: Internet
Author: User
Tags vars

YAF supports user-defined plug-ins to extend the functionality of YAF, which are classes. They must all inherit from Yaf_plugin_abstract. Plug-ins to function, also must be realistic in the YAF register, and then in the appropriate actual, YAF will call it.

Perhaps everyone will ask this plug-in is what concept, what is the use of it, in fact, we use plug-in is mainly used to support the YAF Framework Hook (hook), YAF defined in the 6 hooks, they are:

6 hooks supported in the YAF

1, Routerstartup This will start before the route, that is, the route will call this hook, this is 7 events, the first one. But some of the global custom work, or should be put in the bootstrap to complete

2, Routershutdown This is triggered after the end of the route, it is important to note that the hook will not be triggered until the route is completed correctly.

3, Dispatchloopstartup before the start of the distribution cycle is triggered

4, predispatch before distribution trigger, if in a request processing, the occurrence of forward, then this event will be triggered multiple times

5, Postdispatch After the end of the distribution of the trigger, when the action has been completed, the view has been rendered. Similar to Predispatch, this event may also trigger multiple

6. Dispatchloopshutdown After the end of the distribution loop, this indicates that all business logic has been completed, but the response has not yet been sent

Defining Plugins

The above is just a basic introduction to plug-ins, if you have not contacted a similar design may suddenly be confused, in fact, it does not matter, the most important thing is to remember, there is such an impression, in the follow-up process we use slowly will be able to accept.

The plug-in class is written by the user, but it needs to inherit from Yaf_plugin_abstract. For the plug-in, the 7 hooks mentioned in the previous section do not need to be fully concerned, it only needs to define the method with the same name as the above event in the plug-in class, then this method will be called when the event is triggered.

Instead of the plug-in method, you can accept two parameters, Yaf_request_abstract instances and Yaf_response_abstract instances. A plugin class example is as follows:

[PHP]View Plaincopy
  1. <?php
  2. class Userplugin extends Yaf_plugin_abstract {
  3. Public function Routerstartup (yaf_request_abstract $request, yaf_response_abstract $response) {
  4. }
  5. Public function Routershutdown (yaf_request_abstract $request, yaf_response_abstract $response) {
  6. }
  7. }



Registering plugins

Plug-in to take effect, also need to register with Yaf_dispatcher, then the general plug-in registration will be placed in the Bootstra. An example of registering a plugin is as follows:

[PHP]View Plaincopy
    1. <?PHP&NBSP;&NBSP;
    2. class  Bootstrap extends yaf_bootstrap_abstract{  
    3. &NBSP;&NBSP;
    4.         public  function _initplugin (yaf_dispatcher  $dispatcher)  {  
    5.               $user  = new userplugin ();   
    6.             < Span class= "VARs" > $dispatcher->registerplugin ( $user);   
    7. &NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;}&NBSP;&NBSP;
    8. }  



Plugin Directory

In general, plug-ins should be placed in the plugins directory under Application_path, so that when loading automatically, the loader through the class name, found that this is a plug-in class, will be located in this directory. Of course, the plugin can also be placed in any place you want to prevent, As long as you can load this class in, you can

This is the use of the plug-in process, for the above we do a summary, with the code to speak.

First we define our own plug-in class:

[PHP]View Plaincopy
  1. /**
  2. * Plugin class definition
  3. * userplugin.php
  4. */
  5. Class Userplugin extends Yaf_plugin_abstract {
  6. //Before the route is triggered, this is the oldest of 7 events. But some global customizations should be done in bootstrap.
  7. Public function Routerstartup (yaf_request_abstract $request, yaf_response_abstract $response) {
  8. echo "Plugin Routerstartup called <br/>\n";
  9. }
  10. Triggered after the end of the route, the route must be completed correctly, otherwise this event will not trigger
  11. Public function Routershutdown (yaf_request_abstract $request, yaf_response_abstract $response) {
  12. echo "Plugin Routershutdown called <br/>\n";
  13. }
  14. Triggered before the distribution loop is started
  15. Public function Dispatchloopstartup (yaf_request_abstract $request, yaf_response_abstract $response ) {   
  16. echo "Plugin Dispatchloopstartup called <br/>\n";
  17. }
  18. Triggered before distribution if a forward occurs during a request processing, the event is triggered multiple times
  19. Public function Predispatch (yaf_request_abstract $request, yaf_response_abstract $response) {
  20. echo "Plugin Predispatch called <br/>\n";
  21. }
  22. Triggered after the distribution is finished, the action is finished and the view has been rendered. Similar to Predispatch, this event may also trigger multiple
  23. Public function Postdispatch (yaf_request_abstract $request, yaf_response_abstract $response) { /c3>
  24. echo "Plugin Postdispatch called <br/>\n";
  25. }
  26. //After the distribution loop is triggered, this means that all business logic has been completed, but the response has not yet been sent
  27. Public function Dispatchloopshutdown (yaf_request_abstract $request, yaf_response_abstract $response ) {   
  28. echo "Plugin Dispatchloopshutdown called <br/>\n";
  29. }
  30. Public function Preresponse (yaf_request_abstract $request, yaf_response_abstract $response) {
  31. echo "Plugin preresponse called <br/>\n";
  32. }
  33. }



Then register our plugin, in the previous section we talked about the use of bootstrap, this time we need to use it, that is, in the bootstrap register plugin


  1. Class Bootstrap extends yaf_bootstrap_abstract{
  2. /** 
  3. * Register a plugin
  4. * Plugin directory is in Application_directory/plugins
  5. */
  6. Public function _initplugin (yaf_dispatcher $dispatcher) {
  7. $user = new Userplugin ();
  8. $dispatcher->registerplugin ($user);
  9. }
  10. }



In this way, the plug-in will automatically invoke the relevant hooks during the execution of our project, and we can deploy our own business logic in these hooks, such as whether the user needs to log in, authority to judge, etc.

YAF 0 Basic Learning Summary 7-Learn to use plugins in YAF

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.