Chapter 3: The code structure of the plug-in file

Release time:2018年07月25日 Author:Catfish CMS Reading volume:65267

In the last chapter, we've learned where to put the plug-in, and we'll continue to learn how the actual code is written.


Perhaps a friend  to see their own code to write will find it difficult, it does not  matter, we will continue to uphold the principle of simplicity to make  your work as simple as possible, of course, you still need to have basic  php programming foundation.


let's start!


Let's take a look at the structure of the plug-in file


<?php
/**
 * 插件名: 公告
 * 描述: 公告插件可以在首页的顶部显示一条公告
 * 作者: A.J
 * 版本: V1.0
 * 插件网址: www.catfish-cms.com
 */
namespace app\plugins\announcement; //命名空间

use app\common\Plugin; //父类

class Announcement extends Plugin //继承父类
{
    private $plugin = 'announcement'; //设置插件名
    public function open(&$params)
    {
        //插件开启时执行代码
        ......
    }
    public function close(&$params)
    {
        //插件被关闭时执行代码
        ......
    }
    public function settings(&$params)
    {
        //后台设置,表单页代码
        ......
    }
    public function settings_post(&$params)
    {
        //后台设置,表单提交代码
        ......
    }

    //其他功能代码
    ......
}

The top is a  note, this note is also very important, do not lazy do not write, or use  your plug-in development friends do not know what the plug-in is used.


The format of  the comments is fixed, and several of the comments listed above are  recommended, although you can add other comments, or write less.


The above example is written in Chinese, you can also write your comments in English, as follows


/**
 * Plugin Name: 公告
 * Description: 公告插件可以在首页的顶部显示一条公告
 * Author: A.J
 * Version: V1.0
 * Plugin URI: www.catfish-cms.com
 */

You can directly copy and paste past, and then modify it.


Notes finished, it is necessary to write a namespace. Is the first line of code below the comment.


namespace app\plugins\announcement;

Replace the gray part with your plug-in name, which is the name of the plug-in folder under the "plugins" folder. The name must be the same as the folder, otherwise it will go wrong!


For example, the gray line in the above line of code is the same as the name of the folder where the files are stored

Is the gray part the same as the folder name?


There are friends that do not know what the namespace, it does not matter, directly copied the past, and then modify on the line.


Namespace written, the next is to introduce the parent class, the code is as follows


use app\common\Plugin;

This line of code can be directly copied, and do not need to make any changes.


Then the plug-in class code, as follows


class Announcement extends Plugin
{

    //在这里写代码
   

}
   

Note: The gray part is replaced with the name of your plug-in, with the first letter capitalized. This is very important!


Then we can write the code inside the braces of the class.


The first line of code within the class is


private $plugin = 'announcement';

The role of this code is to set a variable, the contents of the variable is the name of your plug-in, the gray part of your own plug-in name.


With this line of code,  you can reference the plug-in name in the following code, which is  useful in avoiding some conflicts, because you might have several  plug-ins installed on a system, and these plug-ins are made by different  people , Then use the plug-in variables can be a good way to avoid conflicts with other plug-ins, this is a good programming habits. Of course, if your plugin is just for your own use, you can also be lazy.


With this line of code, in the back of the code you can use "$ this-> plugin" to refer to plug-in name.


Then there are two functions


   public function open(&$params)
    {
        //在这里写代码
    }
    public function close(&$params)
    {
        //在这里写代码
    }

The "open" function is the code that executes when the plugin is open, and you can do some initialization work here.


The "close"  function is the code that is executed when the plugin is closed. If you  do not intend to use this plugin, the work will be done when closing the  plugin, which is mainly used to clean up the system and avoid garbage.


Next is your  plug-in background settings code, such as bulletin plug-in, we can write  the contents of the announcement in the background. The code is as follows


   public function settings(&$params)
    {
        //在这里写代码
    }
    public function settings_post(&$params)
    {
        //在这里写代码
    }

"Settings"  function is used to display forms and other content, you need to html  code into the variable $ params ['view'], by, as long as the variable $  params ['view'], your background display work finished. Specifically, you can view the announcement plug-in code.


"Settings_post"  function is the background after the submission form to deal with the  code, where you can perform some work to store data, such as  announcement bulletin in the contents of the announcement is stored with  this function.


Of course, if your plug-ins do not need to set the background, then you can omit the above two functions.


So far, we have  learned the basic structure of plug-in files, then how to display the  contents of the announcement to the home page?


The next chapter  we will learn some function functions, used to achieve the purpose of  displaying the contents of the announcement.


Go to the next chapter


Reward

I want to comment Sign in to post a comment


Latest release

bootstrap4如何实现div的水平居中以及垂直居中

bootstrap是一款十分流行的前端组件库,我们在制作模板的时候,使用bootstra...

鲶鱼CMS的页面设置中如何增减数量

Catfish(鲶鱼) CMS自版本4.9.6开始支持对页面设置可增减操作,我们来看一下...

网站二次开发的正确姿势

经常有用户朋友问我关于二次开发的问题,现在将这个问题比较详细地说一下。 过来问我关于二...

css样式处理超长的连续字母或数字,实现自动换行

有的伙伴建站以后,会在自己的网站分享一些链接,并且有些链接是超长的,由于链接一般都是由字...

绿色目录主题标准版 GreenD theme for Catfish...

主题名称:绿色目录主题标准版 GreenD theme for Catfish-CMS主...

MIME在form表单的文件上传中的应用

我们在制作文件上传时,通常会使用HTML标签“<input type="...

How to use the Catfish CMS system

Section 1: How to add articles

Management background is composed of five bl...

Section 2: to the article to add c...

In the previous section, we talked about how...

Section 3: Add a menu to your site

In the previous section we talked about how ...

Section 4: Page Setup

To the last section we have learned to use t...

Section 5: Change website name, LO...

By the end of the previous section, we have ...

Copyright © 2015 - 2019 Catfish(鲶鱼) CMS
Catfish(鲶鱼) CMS〖181836340〗 Catfish(鲶鱼) CMS 二群〖292598750〗

   I want to leave a message
Catfish(鲶鱼) CMS V 4.9.18