1 制作插件小demo
1.1 新建插件
学习目的:手写一个插件,包含菜单,外界API,模版调用等
1.1.1 搭建插件目录结构
在项目/x/plugin目录下新建一个plugindemo的文件夹,注意文件夹名尽量用小写,避免使用下划线。创建好后,在该文件夹搭建好插件的目录结构,如下图:
1.1.2 配置文件
配置文件即/plugindemo/install/config.php,在里面写上插件扩展名称,插件唯一标示(需要和文件夹名称一致),插件描述,插件版本。
<?php
/**
* @File : config.php
* @Encoding : UTF-8
* @Created on : 2020/7/1 17:14 by lijiwei
* @Copyright : Copyright (c) 2020,jufengcms.com, Inc.
**/
return [
"name"=>"插件demo",//扩展名称
"identifier"=>"plugindemo",//插件唯一标识,需要和文件夹名称一致
"description"=>"这只是个测试的插件demo",
"version"=>"1.0.0",//版本
];
1.1.3 写上入口地址
入口地址重点是href 的值,p的值为插件标识,c 为template目录下的文件夹名,a即为template目录的文件夹下的文件。
<?php
/**
* @File : menu.php
* @Encoding : UTF-8
* @Created on : 2020/7/1 17:13 by lijiwei
* @Copyright : Copyright (c) 2020,jufengcms.com, Inc.
* @Description :
**/
return [
"list"=>[
"name"=>"",
"attribute"=>"",
"href"=>"&p=plugindemo&c=index&a=init",
"entry"=>true,//管理入口地址
]
];
1.1.4 把install.sql和uninstall.sql写完
install.sql 里面写上需要创建的表的sql ,和添加上表所需要的数据,不需可免 。
DROP TABLE IF EXISTS `jufeng_plugin_demo`;
CREATE TABLE `jufeng_plugin_demo` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`name` varchar(255) NOT NULL DEFAULT '' COMMENT '姓名',
`age` int(11) NOT NULL DEFAULT '0' COMMENT '年龄',
`sex` tinyint(4) NOT NULL DEFAULT '0' COMMENT '性别(1男 ,2女)',
`city` varchar(255) NOT NULL DEFAULT '' COMMENT '城市',
PRIMARY KEY (`id`)
) ENGINE=InnoDB AUTO_INCREMENT=1 DEFAULT CHARSET=utf8 COMMENT='测试数据';
INSERT INTO `jufeng_plugin_demo` (`name`,`age`,`sex`,`city`) VALUES ('张三',18, 1, '深圳');
INSERT INTO `jufeng_plugin_demo` (`name`,`age`,`sex`,`city`) VALUES ('李四', 20, 1, '南京');
uninstall.sql里面是卸载时所需执行的sql文件
DROP TABLE IF EXISTS `jufeng_plugin_demo`;
1.1.5 创建model层
在/x/model目录下创建一个plugin_demo.php类文件,也就是MVC开发模式中的模型层,继承/x/classmu目录下的model.php,model.php里面封装了一些基本的对数据表的操作。为了更直观,写了一个queryAll()方法。(注:tbname 的值为表名除去表前缀)
调用queryAll() 的代码为:
(注:M()函数的参数即model层中的tbname)
M('plugin_demo')->queryAll();
1.1.6 创建控制器
在controller目录下创建个index.php类文件,同时写个queryAll()方法为模版提供接口,里面调用model层的数据。
<?php
/**
* @File : index.php
* @Encoding : UTF-8
* @Created on : 2020/7/2 10:56 by lijiwei
* @Copyright : Copyright (c) 2020,jufengcms.com, Inc.
* @Description :
**/
class index
{
public function queryAll(){
$data = M('plugin_demo')->queryAll();
if(empty( $data)){
pluginHelp::export([], 1, '暂无数据');
}
pluginHelp::export(['data'=>$data], 0, '获取成功');
}
}
pluginHelp 里面的export()方法封装的输出json操作,可以直接使用,在/c/class下,函数代码如下:
/**
* 输出json
* @param type $data
* @param type $errno
* @param type $message
* @param type $ext
*/
public static function export($data,$errno=0,$message="ok"){
$errInfo = [
"code"=>$errno,
"msg"=>$message,
];
if(is_object($data)){
$errInfo = array_merge($errInfo, get_object_vars($data));
}elseif(is_array($data)){
$errInfo = array_merge($errInfo, $data);
}else{
$errInfo["data"] = $data;
}
echo json_encode($errInfo);
exit;
}
1.1.7 创建模版
在template模版的index目录下创建init.htm文件,写上如下图代码。系统结合了layui插件,可以直接使用。Xhash 是用户登入时获取的密钥,在系统中一直需要使用。
<title>plagindemo后台模版</title>
<div class="layui-layout layui-layout-main" style="height:calc(100% - 20px)">
<blockquote class="layui-elem-quote jufeng-topMenu jufeng-topBar jufeng-layui-bar jf-lay-top">
<i class="jufeng-icon-weizhi iconfont icon-01weizhi"></i>
<span> 插件<span class="nav-separator">></span>plagindemo后台模版数据表</span>
</blockquote>
<table class="layui-table table-plugindemo-list" id="table-plugindemo-list" lay-filter="table-plugindemo-list"></table>
<script type="text/javascript">
layui.use(['jquery', 'element', 'table', 'form', 'layer', 'laytpl'], function() {
var table = layui.table;
var TAB_PAGE_ID = '{TAB_PAGE_ID}';
var self = $$(TAB_PAGE_ID);
var toolbarHtml = self.find('#toolbarHtml').html();
table.render({
elem: "#table-plugindemo-list",
url: "ajax.php?p=plugindemo&c=index&a=queryAll&xhash=" + xhash,
method: "POST",
where: { pagesize: 10 },
page: true, //开启分页
parseData: function(res) {
return {
"code": res.code, //解析接口状态
"msg": res.msg, //解析提示文本
"count": res.count, //解析数据长度
"data": res.data //解析数据列表
};
},
limit: 10,
cols: [
[ //表头
{
field: 'id',
title: 'ID',
width: 120,
sort: true,
}, {
field: 'name',
title: '姓名',
// width: 200
}, {
field: 'age',
title: '年龄',
// width: 180
}, {
field: 'sex',
title: '性别',
// width: 180
}, {
field: 'city',
title: '城市',
// width: 180
}
]
]
});
});
</script>
</div>
到此,编码阶段基本完成,还剩模版调用和对外API 后面继续,先把插件代码都上传的服务器或就在本地。看看效果。
2 上传并安装插件
把写好的插件在项目中放好位置后,就可打开系统后台。找到插件下的所有插件栏目,就可以看到我们刚新建的插件的
点击安装,会弹出一个对话框,并且给你的插件加了个随机字符的后缀,目的是为了提升系统的安全性,防止他人可以随意的找你目录下的文件。然后继续点击安装,安装程序就结束了。
之后我们可以找到我们安装的插件,点开插件名称,就可以看到我们所写的模版界面。注意模版文件名一定要和menu.php里的一致,否者就会出现模版文件不存在的情况。
那出现这种情况的时候,我们需要改menu.php和安装时在/d/plugin_config/下自动生成的配置文件,文件名为 (插件唯一标识).conf.php。这里就是 plugindemo.conf.php了。把entry 值的a参数改成和模版文件名一样的就行了。
<?php
return array (
'available' => 1,
'adminid' => '59',
'name' => '插件demo',
'identifier' => 'plugindemo',
'description' => '这只是个测试的插件demo',
'copyright' => '',
'version' => '1.0.0',
'install_time' => '2020-07-02 14:25:54',
'entry' => '&p=plugindemo&c=index&a=init',
'dir' => 'plugindemo_JcV771',
);
一切都弄好以后,就可看到我们的界面了。这里可以写些对数据的增删改查操作,当然一切根据自己需求。
3 对外API
某些插件可能需要给外界提供一些接口API,在controller 里是访问不到的,所以我们可以写在actions里。在actions里建个index,php类文件。方法可以和controller里一样。代码如下:
<?php
/**
* @File : index.php
* @Encoding : UTF-8
* @Created on : 2020/7/2 16:33 by lijiwei
* @Copyright : Copyright (c) 2020,jufengcms.com, Inc.
* @Description :
**/
class index
{
public function queryAll(){
$data = M('plugin_demo')->queryAll();
if(empty( $data)){
pluginHelp::export([], 1, '暂无数据');
}
pluginHelp::export(['data'=>$data], 0, '获取成功');
}
}
直接在浏览器上访问,路径为:
域名/plugin/p_插件标识/c_(actions目录下的文件名)/a_(文件里的方法)
4 系统模版调用
系统模版里面需要的数据又是可以用php调用出来,我们把方法写在 plugindemo_data.php 类里面。
<?php
/**
* @File : plugindemo_data.php
* @Encoding : UTF-8
* @Created on : 2020/7/1 17:13 by lijiwei
* @Copyright : Copyright (c) 2020,jufengcms.com, Inc.
* @Description :
**/
class plugindemo_data
{
public function queryAll(){
$data M('plugin_demo')_queryAll();
return $data;
}
}
在系模版里,需要借助 /x/class/下的f类文件,调用里面的plugin()方法,plugin方法有三个参数,第一个是:插件标识,第二个是(件标识)_data.php里的某个方法名,第三个则是需要传递的参数。
$user = f::plugin('plugin_demo','queryAll',[]);
到此,开发实例结束。