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 apppluginsannouncement; //命名空间
use appcommonPlugin; //父类
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 apppluginsannouncement;
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 appcommonPlugin;
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.
Original article, reproduced please specify:Chapter 3: The code structure of the plug-in file | Catfish(鲶鱼) CMS
我们在做主题的时候,有时需要在页面单独显示当前菜单下的二级菜单,这个在鲶鱼系统里面有两种...
由于PHP软件的运行都需要服务器运行环境,虽然可以找到很多PHP的集成环境来使用,但是多...
我们在使用博客网站的时候,经常会希望对自己发布的文章进行归档处理,例如每个月发布了哪些文...
鲶鱼系统可以使用找回密码功能,但是在开启找回密码功能之前,我们必须要进行一项设置,就是S...
现代程序开发多是使用框架来写应用程序,因为框架可以带来事半功倍的效果。对于PHP程序也是...
鲶鱼cms系统5.2.0版以及鲶鱼Blog系统3.2.0版在编辑文章页面新增了上传附件功...
Management background is composed of five bl...
In the previous section, we talked about how...
In the previous section we talked about how ...
To the last section we have learned to use t...
By the end of the previous section, we have ...