Extend control

2018-07-16 09:44:51
tengfei
806
Last edited by tengfei on 2019-01-08 09:29:41

There are two extensions to the control of existing modules, one is to override existing methods, the other is to add new methods. Now let's look at how to do it.


1. Naming of Files

No matter it is to override existing methods or to add new methods, the extension files are saved under the ext/control directory named after the method in lowercase.

For example, the user module. If you want to redefine its registration logic, create register.php in module/user/ext/control and then implement the code.

If you want to add an open login to the user module, let's say "OAuth", create oauth.php under module/user/ext/control and then implement the code.


2. Extend codes independent

When extending the control layer, it can be completely independent, or the method defined by the trunk code in the control layer can be used. The following example is completely independent.


class user extends control
{
    public function register()
    {
        $this->view->header->title = 'getsid';
        $this->view->sid = session_id();
        $this->view->test = $this->misc->test();
        $this->display();
    }
} 


Pay attention to the definition of class name: user, derived from the control base class. Such a definition is completely independent.



3.  Inheritance

The example above is an independent extension. If you still want to use the original code of our products, you can do it via inheritance.


include '../../control.php'; 
class myUser extends user
{
    public function register()
   {
        ....
        $this->process()    // process is defined in ../../control.php
   }
}


First, you need to manually create ../../control.php, and then define the class name as myUser (my + module name) which is derived from the user class, so that the process method can be defined in the../../control.php method and it can be called in the register method.


4. Restriction

Because of the limitation to the loading mechanism, one control method can only have one extension.


Write a Comment
Comment will be posted after it is reviewed.