PHPCMS V9 模块开发 二次开发实例 留言本

对于像我这样的入门者希望先把上面这个文档仔细读一遍再往下看!

声明:我用的是GBK版本。

二次开发流程

  • 创建数据库和数据库表(无数据库操作可略过)
  • 创建数据模型文件
  • 创建模块目录
  • 开发控制器和模板
  • install和uninstall模块
  • 一、创建数据库表

    具体需求请查看上面的文档,不再赘述直接上SQL语句:

    复制代码 DROP TABLE IF EXISTS `guestbook`;CREATE TABLE IF NOT EXISTS `guestbook` (
      `gid` smallint(5) NOT NULL AUTO_INCREMENT,
      `siteid` smallint(5) NOT NULL,
      `title` char(80) NOT NULL,
      `content` text NOT NULL,
      `reply` text NOT NULL,
      `userid` mediumint(8) unsigned NOT NULL DEFAULT '0',
      `username` char(20) NOT NULL,
      `passed` tinyint(1) unsigned NOT NULL DEFAULT '0',
      `reply_status` tinyint(1) unsigned NOT NULL DEFAULT '0',
      `addtime` int(10) unsigned NOT NULL DEFAULT '0',
      `replyer` char(20) NOT NULL,
      `replytime` int(10) unsigned NOT NULL DEFAULT '0',
      PRIMARY KEY (`gid`)
    )DEFAULT CHARSET=utf8;
    复制代码  

    二、创建数据模型文件

    数据库模型位于:phpcms/model/ 目录下。数据模型文件的命名规则建议为 '数据表名称' + '_model.class.php' 。

    这个模块中我们要使表“guestbook”,则数据库模型文件名称为'guestbook_model.class.php',程序如下:

    复制代码 php defined('IN_PHPCMS') or exit('No permission resources.'); pc_base::load_sys_class('model', '', 0); class guestbook_model extends model {public function __construct() {$this->db_config = pc_base::load_config('database');$this->db_setting = 'default';// 记得换成自己的表名$this->table_name = 'guestbook';parent::__construct();} } ?> 复制代码 说明:任何自定义模块的数据模型类,均继承于model.class.php 数据模型基类。

    在此基类中PHPCMS 系统已经把最常用的数据库操作方法进行了封装。 二次开发者不必关于如何操作数据库,

    只需要根据需要用到的,已定义操作方法的要求,传递参数即可。系统会自动对数据进行处理,并返回结果。

    说白了就是对你自定义的数据表的包装,更方便操作数据库。

     

    三、创建模块目录  

    PHPCMS v9框架中的模块,位于phpcms/modules目录中,每一个目录称之为一个模块。

    如果要创建一个新模块,只要在 phpcms/modules 目录下创建文件夹并放入你的控制器类就可以了。

    当前我们要开发一个叫做guestbook的留言本模块,那么首先在 phpcms/modules 目录下创建文件夹,

    并将其命名为guestbook。如下图所示:

    guestbook 模块的标准结构通常是这样的,如下图所示:

    classes  为模块类文件夹

    functions 为模块函数文件夹

    templates 为模块模板文件夹,这里通常放置含有权限控制的控制器模板,也就是后台模板!!!

    如果您的模块有单独的前台模版,你需要在phpcms/templates/default下,

    创建一个您的模块同名目录来放置前台模板(并进行配置,后面会说到),“default”为你的风格包名称,我们默认适用default。

    install和uninstall为模块安装和卸载模块

     

    四、开发控制器和模板

    PHPCMS V9的控制器位于phpcms/modules/模块/目录下面,不是classes目录下。文件名是类名+.php,

    例如一个名为guestbook的控制器,那么他的命名为guestbook.php即可。控制器类默认继承系统的函数库,可以直接使用。

    需要注意的是:控制器类的类名称与控制器文件名必须相同本留言本模块有以下二个控制器:

  • 前台index.php控制器开发
  •   前台控制器主要涉及前台留言显示、留言的提交处理等功能函数,以下为全部源代码,代码如下所示:  

    复制代码 php defined('IN_PHPCMS') or exit('No permission resources.'); class index {function __construct() {// 加载留言本的数据模型$this->guestbook_db = pc_base::load_model('guestbook_model');// 取得当前登录会员的会员名(username)和会员ID(userid)$this->_username = param::get_cookie('_username');$this->_userid = param::get_cookie('_userid');//定义站点ID常量,选择模版使用$siteid = isset($_GET['siteid']) ? intval($_GET['siteid']) : get_siteid();define("SITEID", $siteid);//读取配置$setting = new_html_special_chars(getcache('guestbook', 'commons'));$this->set = $setting[SITEID];}public function init() {//设置分页条数$pagesize = $this->set['pagesize'];$where = array('passed'=>1,'siteid'=>SITEID);$page = isset($_GET['page']) && intval($_GET['page']) ? intval($_GET['page']) : 1;$infos = $this->guestbook_db->listinfo($where, 'gid DESC', $page, $pagesize);$infos = new_html_special_chars($infos);// 加载系统form类,用于前台模块文件中生成验证码pc_base::load_sys_class('form', '', 0);// 加载前台模板include template('guestbook', 'index');}/*** 在线留言 */public function ly() {if(isset($_POST['dosubmit'])){ // 标题和内容不能为空if (!(isset($_POST['ly']['title']) && trim($_POST['ly']['title']) && isset($_POST['ly']['content']) && trim($_POST['ly']['content']))) {showmessage(L('输入不能为空'), "?m=guestbook&c=index&siteid=".SITEID);}// 验证码if(isset($_POST['code'])){/*V9验证码的数值是通过SESSION传递,故在这段代码中,首先加载配置文件, 取出当前系统配置中SESSION的存储方式。然后根据SESSION的存储方式,来加载对应的系统类库*/$session_storage = 'session_'.pc_base::load_config('system','session_storage');pc_base::load_sys_class($session_storage);if(!isset($_SESSION)) {session_start();}$code = isset($_POST['code']) && trim($_POST['code']) ? trim($_POST['code']) : showmessage(L('请输入验证码'), HTTP_REFERER);if ($_SESSION['code'] != strtolower($code)) {showmessage(L('验证码错误'), HTTP_REFERER);}} $set = $this->set;$_POST['ly']['addtime'] = SYS_TIME;$_POST['ly']['userid'] = $this->_userid;$_POST['ly']['username'] = $this->_username;$_POST['ly']['siteid'] = SITEID;$_POST['ly']['passed'] = $set['check_pass'];$this->guestbook_db->insert($_POST['ly']);showmessage(L('添加成功'), "?m=guestbook&c=index&siteid=".SITEID);} else {echo '请通过正常的方式提交留言,谢谢';}} } ?> 复制代码   前台模板

      第三部分我们已经说了需要在phpcms/templates/default下创建一个'guestbook'目录,在该目录下再创建index.html文件,其源码如下:  

    复制代码 {if $this->set['guestbook_status']} <div class="box boxsbg cboxs"><h5>我要留言 h5><div class="tag_a"><form action="{APP_PATH}index.php?m=guestbook&c=index&a=ly&siteid={SITEID}" method="post" name="myform" id="myform"><table cellspacing="1" cellpadding="0" class="table_form"><tbody> <tr> <th>标题th><td><input type="text" value="" id="title" name="ly[title]" class="input-text">td>tr><tr> <th>内容th><td> <textarea name="ly[content]" id="content" cols="5" rows="8" value="" style="width:400px;">textarea>td>tr>{if $this->set['enablecheckcode']==1}<tr> <th>验证码:th><td> <input name="code" type="text" id="code"/>{form::checkcode('code_img','4','14',110,30)}td>tr>{/if}<tr> <th>th><td><input type="submit" value="提交" name="dosubmit" class="button"><input type="reset" value=" 取消" name="reset" class="button"> td>tr> tbody>table> form>div> div> <div style="height:5px;">div> {/if}php if(is_array($infos)){ foreach($infos as $info){ ?> <div class="box boxsbg cboxs"><h5>{$info['title']}h5><div class="tag_a">{$info['content']}<br><br>{if $info['reply']}<font color=red>回复内容:font><font style="color: #09F;">{$info['reply']}font>{/if}div> div> <div style="height:5px;">div> php }} ?> 复制代码   打开phpcms/templates/default/config.php , 进行以下两处修改:

      

  • 后台guestbook.php控制器开发
  •   后台管理控制器含权限控制,只有特定管理员才有权限访问,所以这个控制器需要加载admin 模块下的admin类,并继承该类。代码如下:  

    复制代码 php
    defined('IN_PHPCMS') or exit('No permission resources. - guestbook.php'); pc_base::load_app_class('admin', 'admin', 0);class guestbook extends admin {public function __construct() {parent::__construct();//继承父类构造函数$setting = new_html_special_chars(getcache('guestbook', 'commons'));//读取留言本配置缓存文件$this->set = $setting[$this->get_siteid()];$this->guestbook_db = pc_base::load_model('guestbook_model');//加载留言本数据模型 }public function init() {$where = array('siteid'=>$this->get_siteid());$page = isset($_GET['page']) && intval($_GET['page']) ? intval($_GET['page']) : 1;$infos = $this->guestbook_db->listinfo($where, 'gid DESC', $page, '15');/* 加载后台管理模版 guestbook_list.tpl.php。 * 此文件位于phpcms/modules/模块/templates/ * 由此即可明白,其它后台管理模版亦位于此目录*/include $this->admin_tpl('guestbook_list');}/* 未回复列表 */public function unreplylist() {$where = array('reply'=>'','siteid'=>$this->get_siteid()); $page = isset($_GET['page']) && intval($_GET['page']) ? intval($_GET['page']) : 1;$infos = $this->guestbook_db->listinfo($where, 'gid DESC', $page, '15');include $this->admin_tpl('guestbook_list');}/*** 回复留言*/public function reply() {if(isset($_POST['dosubmit'])){$gid = intval($_GET['gid']);if($gid < 1) return false; $_POST['reply']['replytime'] = SYS_TIME;$_POST['reply']['reply_status'] = '1';$this->guestbook_db->update($_POST['reply'], array('gid'=>$gid)); showmessage(L('回复成功'),'?m=guestbook&c=guestbook&a=init');} else {$gid = intval($_GET['gid']);if($gid < 1) return false; $show_validator = $show_scroll = $show_header = true;$info = $this->guestbook_db->get_one(array('gid'=>$_GET['gid']));if(!$info) showmessage(L('guestbook_exit'),'?m=guestbook&c=guestbook&a=init');extract($info); // 加载后台管理模版 guestbook_reply.tpl.phpinclude $this->admin_tpl('guestbook_reply');}}/*** 删除留言 * @param intval $gid 留言ID,递归删除*/public function delete() {if((!isset($_GET['gid']) || empty($_GET['gid'])) && (!isset($_POST['gid']) || empty($_POST['gid']))) {showmessage(L('未选中'), HTTP_REFERER);}if(is_array($_POST['gid'])){foreach($_POST['gid'] as $gid_arr) {$gid_arr = intval($gid_arr);$this->guestbook_db->delete(array('gid'=>$gid_arr)); }showmessage(L('删除成功'),'?m=guestbook&c=guestbook');}else{$gid = intval($_GET['gid']);if($gid < 1) return false;$result = $this->guestbook_db->delete(array('gid'=>$gid));if($result){showmessage(L('删除成功'),'?m=guestbook&c=guestbook');}else {showmessage(L("删除失败"),'?m=guestbook&c=guestbook');}}}/*** 留言本模块配置*/public function setting() {//更新模型数据库,重设setting 数据. $m_db = pc_base::load_model('module_model');$set = $m_db->get_one(array('module'=>'guestbook'));$setting = string2array($set['setting']);$now_setting = $setting[$this->get_siteid()];//当前站点的配置if(isset($_POST['dosubmit'])) {$setting[$this->get_siteid()] = $_POST['setting'];setcache('guestbook', $setting, 'commons'); $set = array2string($setting);$m_db->update(array('setting'=>$set), array('module'=>ROUTE_M));showmessage('配置更新成功', '?m=guestbook&c=guestbook&a=init');} else {extract($now_setting);// 加载后台管理模版 setting.tpl.phpinclude $this->admin_tpl('setting');}} } ?> 复制代码   上面涉及的后台模板代码如下:

      guestbook_list.tpl.php  

    复制代码 php
    defined('IN_ADMIN') or exit('No permission resources. - guestbook_list.tpl.php'); $show_dialog = 1; include $this->admin_tpl('header', 'admin'); ?>
    phpif(is_array($infos)){foreach($infosas$info){?>
    标题内容姓名发表时间是否回复管理操作
    echo $info['title']?>echo $info['content']?>echo $info['username'];?>echo date('Y-m-d H-i-s',$info['addtime']);?>if($info['reply']==''){echo '未回复';}else{echo '已回复';}?>" title="回复留言">回复 |'onClick="return confirm(' new_addslashes($info['title'])))?>')">echo L('删除')?>

     &nbsp;
    复制代码   guestbook_reply.tpl.php  

    复制代码 php
    defined('IN_ADMIN') or exit('No permission resources. - guestbook_reply.tpl.php'); $show_dialog = 1; include $this->admin_tpl('header', 'admin'); ?>
    class="table_form" width="100%">
    echo L('username')?>:echo $username;?>
    标题:echo $title;?>
    内容:echo $content;?>
    留言时间:echo date('Y-m-d H:i:s', $addtime);?>
    回复内容:
    回复时间:echo date("Y-m-d H:i:s", time());?>
    是否通过审核:if($passed==1){echo "checked";}?>> echo L('yes')?> &nbsp;if($passed==0){echo "checked";}?>> echo L('no')?>
    复制代码   setting.tpl.php

    复制代码 php
    defined('IN_ADMIN') or exit('No permission resources.'); $show_dialog = 1; include $this->admin_tpl('header', 'admin'); ?>
    class="table_form" width="100%">
    是否允许留言: if($guestbook_status==1){echo "checked";}?> /> echo L('yes')?> &nbsp; if($guestbook_status==0){echo "checked";}?> /> echo L('no')?>
    是否允许游客留言: if($allow_guest==1){echo "checked";}?> /> echo L('yes')?> &nbsp; if($allow_guest==0){echo "checked";}?> /> echo L('no')?>
    是否需要审核: if($check_pass==1){echo "checked";}?> /> echo L('yes')?> &nbsp; if($check_pass==0){echo "checked";}?> /> echo L('no')?>
    每页条数:
    是否开启验证码: if($enablecheckcode==1){echo "checked";}?> /> echo L('yes')?> &nbsp; if($enablecheckcode==0){echo "checked";}?> /> echo L('no')?>
    复制代码  

    五、安装模块和卸载模块

    首先确保你的安装和卸载目录如下:

    具体配置可以参考这篇帖子phpcms v9添加新模块。我们这里:

    guestbook.lang.php -- 内容为空就行了

    config.inc.php -- 模块信息和作者信息

    复制代码 php
    defined('IN_PHPCMS') or exit('Access Denied'); defined('INSTALL') or exit('Access Denied');$module = 'guestbook'; $modulename = '留言板'; $introduce = '留言板独立模块'; $author = '你的名字'; $authorsite = 'http://www.nidewangzhi.cn'; $authoremail = 'nideyouxiang@163.com'; ?> 复制代码 extention.inc.php -- 后台管理菜单

    复制代码 php
    defined('IN_PHPCMS') or exit('Access Denied'); defined('INSTALL') or exit('Access Denied');$parentid = $menu_db->insert(array('name'=>'guestbook', 'parentid'=>29, 'm'=>'guestbook', 'c'=>'guestbook', 'a'=>'init', 'data'=>'', 'listorder'=>0, 'display'=>'1'), true); $menu_db->insert(array('name'=>'setting', 'parentid'=>$parentid, 'm'=>'guestbook', 'c'=>'guestbook', 'a'=>'setting', 'data'=>'', 'listorder'=>0, 'display'=>'1')); $menu_db->insert(array('name'=>'unreply', 'parentid'=>$parentid, 'm'=>'guestbook', 'c'=>'guestbook', 'a'=>'unreplylist', 'data'=>'', 'listorder'=>0, 'display'=>'1'));$language = array('guestbook'=>'留言本', 'setting'=>'留言配置', 'unreply'=>'未回复留言'); ?> 复制代码 guestbook.sql -- 安装时数据库表的添加

    复制代码 DROP TABLE IF EXISTS `guestbook`;CREATE TABLE IF NOT EXISTS `guestbook` (
      `gid` smallint(5) NOT NULL AUTO_INCREMENT,
      `siteid` smallint(5) NOT NULL,
      `title` char(80) NOT NULL,
      `content` text NOT NULL,
      `reply` text NOT NULL,
      `userid` mediumint(8) unsigned NOT NULL DEFAULT '0',
      `username` char(20) NOT NULL,
      `passed` tinyint(1) unsigned NOT NULL DEFAULT '0',
      `reply_status` tinyint(1) unsigned NOT NULL DEFAULT '0',
      `addtime` int(10) unsigned NOT NULL DEFAULT '0',
      `replyer` char(20) NOT NULL,
      `replytime` int(10) unsigned NOT NULL DEFAULT '0',
      PRIMARY KEY (`gid`)
    )DEFAULT CHARSET=utf8;
    复制代码 model.php -- 你所使用到的数据表

    复制代码 php
    defined('IN_PHPCMS') or exit('Access Denied'); defined('INSTALL') or exit('Access Denied');return array('guestbook'); ?> 复制代码 module.sql --

    复制代码 INSERT INTO `module` (`module`, `name`, `url`, `iscore`, `version`, `description`, `setting`, `listorder`, `disabled`, `installdate`, `updatedate`) VALUES ('guestbook', '留言本', 'guestbook/', 0, '1.0', '留言本', 'array ("1" => array ("guestbook_status" => "1",
        "allow_guest" => "1",
        "check_pass" => "1",
        "pagesize" => "3",
        "enablecheckcode" => "0",
    ),"2" => array ("guestbook_status" => "1","allow_guest" => "1","check_pass" => "1","pagesize" => "5","enablecheckcode" => "0",), )', 0, 0, '2014-7-18', '2014-7-18'); 复制代码  卸载模块里面更是照葫芦画瓢了,不在赘述。

    六、后续

      在后台安装这个模块,本人水平有限,帖子难免有许多纰漏,肯定会有很多细节没有讲清楚,大家可以先仔细阅读开篇的那个文档,再和帖子结合理解。界面如下:

      

    谢谢围观!


    本文来自互联网用户投稿,文章观点仅代表作者本人,不代表本站立场,不承担相关法律责任。如若转载,请注明出处。 如若内容造成侵权/违法违规/事实不符,请点击【内容举报】进行投诉反馈!

    相关文章

    立即
    投稿

    微信公众账号

    微信扫一扫加关注

    返回
    顶部