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

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

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

phpStudy安装鲶鱼CMS怎样开启伪静态

phpStudy是一款PHP运行环境的集成包,为了方便,我们在调试或者运行PHP程序的时...

Refuting the article &quot;Not...

Today, users of Catfish CMS told me that the...

使用mysql命令解决phpMyAdmin在MySQL8下面无法登录...

MySQL是一款优秀的数据库系统,有大量的应用会使用MySQL,截止本文发布,MySQL...

如何给导航菜单添加图标

我们访问某些网站的时候,会发现导航菜单的文字前面有一个小图标,如下图所示: 在鲶鱼系统...

bootstrap4导航菜单的二级下拉菜单鼠标悬停显示jQuery怎...

bootstrap的导航菜单默认都是通过鼠标点击来展开的,有时我们为了让浏览者有更好的访...

jQuery如何获取当前元素的ID

jQuery是一个优秀的JavaScript库,给前端页面的制作带来了极大的方便,鲶鱼系...

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.36