第一个插件
第一步,插件创建
1、打开插件中心进行管理
最新版的插件中心已经显示在左侧菜单栏中,如果您没有找到,请点右上角,切换到【开发模式】,再找到【设置】或【工具】里可以找到【插件中心】
2、创建插件
点击【创建插件】,填写插件名称,标识,用途及开发者,点【确定】即可创建成功一个插件。
第二步,了解插件结构
通过第一步,我们创建了一个【演示用的】插件,切换到目录 plugins 里,我们可以看到一个【demo】的文件
点击进去查看,可以看到如下的结构
其中 config.xml 为配置文件,生成的代码如下:
<?xml version="1.0" encoding="utf-8"?> <root> <title>演示插件</title> <desc>这是一个用于演示的插件</desc> <author>phpok.com</author> <version>1.0</version> </root>
配置文件用于系统后台识别,其字段分别表示:
title:插件的名称,您可以使用您自己习惯的语言来编写
desc:插件描述,介绍这个插件是做什么的
author:插件的作者
version:插件版本,默认为1.0
配置文件在后台可以正常识别到效果如下:
注:插件安装完成后,该config.xml文件就不被调用了,数据存到数据库中!
template | 插件中涉及到的模板目录 |
admin.php | 后台管理专用,所有的后台入口均已验证过管理员,无需再限制 |
api.php | 接口插件专用,默认没有任何限制,任何要求,为确保数据安全,请慎重安装第三方插件 |
config.xml | 配置文件 |
install.php | 插件安装执行的脚本文件,包括数据导入都可以写到这里来。非必须的,部份插件很简单,没有什么扩展,所以可以省略此文件 |
setting.php | 安装后的配置参数 |
uninstall.php | 卸载插件时执行的脚本文件 |
www.php | 前台读写专用插件 |
第三步、编写 install.php 文件
<?php /** * 演示插件<插件安装> * @package phpok\plugins * @作者 phpok.com * @版本 5.4 * @授权 http://www.phpok.com/lgpl.html PHPOK开源授权协议:GNU Lesser General Public License * @时间 2019年12月05日 22时29分 **/ class install_demo extends phpok_plugin { public $me; public function __construct() { parent::plugin(); $this->me = $this->_info(); } /** * 插件安装时,增加的扩展表单输出项,如果不使用,请删除这个方法 **/ public function index() { //return $this->_tpl('setting.html'); } /** * 插件安装时,保存扩展参数,如果不使用,请删除这个方法 **/ public function save() { $id = $this->_id(); $ext = array(); //$ext['扩展参数字段名'] = $this->get('表单字段名'); $this->_save($ext,$id); } }
方法 index()
表示在执行安装时输出操作,可以在这里读取项目信息,列表及各种SQL信息。我们这里扩展保存一个 demo1 的字段
public function index() { return $this->_tpl('setting.html'); }
编写模板 setting.html
<div class="table"> <div class="title"> 插件扩展字段1:<span class="note">这个是插件的扩展字段1</span> </div> <div class="content"> <input type="text" name="demo1" id="demo1" value="{$rs.param.demo1}" /> </div> </div>
效果图如下:
方法 save()
表示提交安装后执行的操作,其中要扩展的字段可以通过
$ext['扩展参数字段名'] = $this->get('表单字段名');
来实现数据的保存,如下图
/** * 插件安装时,保存扩展参数,如果不使用,请删除这个方法 **/ public function save() { $id = $this->_id(); $ext = array(); $ext['demo1'] = $this->get('demo1'); $this->_save($ext,$id); }
同时可以在 $this->_save($ext,$id); 之后添加自己的执行操作,如导入SQL,如:
/** * 插件安装时,保存扩展参数,如果不使用,请删除这个方法 **/ public function save() { $id = $this->_id(); $ext = array(); $ext['demo1'] = $this->get('demo1'); $this->_save($ext,$id); $sql = "UPDATE *** SET *** WHERE ***"; $this->db->query($sql); }
第四步,配置 setting.php 文件
<?php /** * 演示插件<插件配置> * @package phpok\plugins * @作者 phpok.com * @版本 5.4 * @授权 http://www.phpok.com/lgpl.html PHPOK开源授权协议:GNU Lesser General Public License * @时间 2019年12月05日 22时29分 **/ class setting_demo extends phpok_plugin { public $me; public function __construct() { parent::plugin(); $this->me = $this->_info(); } /** * 插件配置参数时,增加的扩展表单输出项,如果不使用,请删除这个方法 **/ public function index() { //return $this->_tpl('setting.html'); } /** * 插件配置参数时,保存扩展参数,如果不使用,请删除这个方法 **/ public function save() { //$id = $this->_id(); //$ext = array(); //$ext['扩展参数字段名'] = $this->get('表单字段名'); //$this->_save($ext,$id); } /** * 插件执行审核动作时,执行的操作,如果不使用,请删除这个方法 **/ public function status() { //执行一些自定义的动作 } }
总的来说,setting.php 文件和 install.php 相似,方法 index() 和 save() 方法完全是一样的。这里重点讲解下方法 status()。
方法 status()
这个方法是插件自启用或禁用时调用的!如果【禁用】或【启用】当前插件,可以通过 status() 联动触发一些其他操作,如启用后同时创建一个快捷菜单,禁用后关闭这个快捷菜单。
/** * 插件执行审核动作时,执行的操作,如果不使用,请删除这个方法 **/ public function status() { //执行一些自定义的动作 $rs = $this->_info(); if($rs['status']){ //开启状态 }else{ //结束状态 } }
第五步,安装插件
点击【安装】,进入安装界面
点击【提交】
我们用数据库管理工具phpmyadmin查看数据是否存储进来,如下图:
第六步,关于 admin.php
<?php /** * 演示插件<后台应用> * @作者 phpok.com * @版本 5.4 * @授权 http://www.phpok.com/lgpl.html PHPOK开源授权协议:GNU Lesser General Public License * @时间 2019年12月05日 22时29分 **/ class admin_demo extends phpok_plugin { public $me; public function __construct() { parent::plugin(); $this->me = $this->_info(); } /** * 全局运行插件,在执行当前方法运行前,调整参数,如果不使用,请删除这个方法 **/ public function phpok_before() { //PHP代码; } /** * 全局运行插件,在执行当前方法运行后,数据未输出前,如果不使用,请删除这个方法 **/ public function phpok_after() { //PHP代码; } /** * 系统内置在</head>节点前输出HTML内容,如果不使用,请删除这个方法 **/ public function html_phpokhead() { //$this->_show("phpokhead.html"); } /** * 系统内置在</body>节点前输出HTML内容,如果不使用,请删除这个方法 **/ public function html_phpokbody() { //$this->_show("phpokbody.html"); } /** * 更新或添加保存完主题后触发动作,如果不使用,请删除这个方法 * @参数 $id 主题ID * @参数 $project 项目信息,数组 * @返回 true **/ public function system_admin_title_success($id,$project) { //PHP代码; } }
第七步,关于 api.php
<?php /** * 演示插件<接口应用> * @作者 phpok.com * @版本 5.4 * @授权 http://www.phpok.com/lgpl.html PHPOK开源授权协议:GNU Lesser General Public License * @时间 2019年12月05日 22时29分 **/ class api_demo extends phpok_plugin { public $me; public function __construct() { parent::plugin(); $this->me = $this->_info(); } /** * 全局运行插件,在执行当前方法运行前,调整参数,如果不使用,请删除这个方法 **/ public function phpok_before() { //PHP代码; } /** * 全局运行插件,在执行当前方法运行后,数据未输出前,如果不使用,请删除这个方法 **/ public function phpok_after() { //PHP代码; } }
第八步,关于 www.php
<?php /** * 演示插件<前台应用> * @作者 phpok.com * @版本 5.4 * @授权 http://www.phpok.com/lgpl.html PHPOK开源授权协议:GNU Lesser General Public License * @时间 2019年12月05日 22时29分 **/ class www_demo extends phpok_plugin { public $me; public function __construct() { parent::plugin(); $this->me = $this->_info(); } /** * 全局运行插件,在执行当前方法运行前,调整参数,如果不使用,请删除这个方法 **/ public function phpok_before() { //PHP代码; } /** * 全局运行插件,在执行当前方法运行后,数据未输出前,如果不使用,请删除这个方法 **/ public function phpok_after() { //PHP代码; } /** * 系统内置在</head>节点前输出HTML内容,如果不使用,请删除这个方法 **/ public function html_phpokhead() { //$this->_show("phpokhead.html"); } /** * 系统内置在</body>节点前输出HTML内容,如果不使用,请删除这个方法 **/ public function html_phpokbody() { //$this->_show("phpokbody.html"); } /** * 针对不同项目,配置不同的主题查询条件,如果不使用,请删除这个方法 * @参数 $project 项目信息,数组 * @参数 $module 模块信息,数组 * @返回 $dt数组或false **/ public function system_www_arclist($project,$module) { //$dt = array(); //$dt["fields"] = "id,thumb"; //$this->assign("dt",$dt); } }
第九步,卸载 uninstall.php
<?php /** * 演示插件<插件卸载> * @作者 phpok.com * @版本 5.4 * @授权 http://www.phpok.com/lgpl.html PHPOK开源授权协议:GNU Lesser General Public License * @时间 2019年12月05日 22时29分 **/ class uninstall_demo extends phpok_plugin { public $me; public function __construct() { parent::plugin(); $this->me = $this->_info(); } /** * 插件卸载时,执行的方法,如删除表,或去除其他一些选项,如果不使用,请删除这个方法 **/ public function index() { //执行一些自定义的动作 } }
方法 index() 指在卸载过程中同时执行的方法