Phpcms_V9 【test】测试
================================================================
================================================================
一:创建数据库和数据表
创建test_model.class.php
数据库模型位于:phpcms/model/目录下
命名规则:test_model.class.php
phpcms/model/test_model.class.php此文件代码:
<?php
defined('IN_PHPCMS') or exit('No permission resources.');
pc_base::load_sys_class('model', '', 0);
class test_model extends model {
public function __construct() {
$this->db_config = pc_base::load_config('database');
$this->db_setting = ‘default';
$this->table_name = 'test';
parent::__construct();
}
}
?>
---------------------------------------------------------------------------------------------
二:创建模块目录
路径:phpcms/modules 下创建文件夹【test】(并放入你的控制器类就可以了)
如果你有一个单独的前台模板:需要在phpcms/templates/default下创建一个模块目录来放置前台模板
“default”——为你的风格包名称,默认使用default
..../index.php?m=test(访问test模块示例:)
---------------------------------------------------------------------------------------------
三:开发模块控制器类
为test模块增加一个名为mytest的控制器类
文件路径:phpcms/modules/test/mytest.php
<?php
defined('IN_PHPCMS') or exit('No permiis sion resources');
class mytest
{
public function init(){
echo 'this is init';
}
public function mylist(){
echo 'this is mylist<br>hello world';
}
}
?>
---------------------------------------------------------------------------------
四:创建数据表
create TABLE v9_test(
id int auto_increment primary key,
title char(20)
);
insert into v9_test values(null,'this is a test');
insert into v9_test values(null,'hello world')
-----------------------------------------------------------------------------------
五:修改控制器test/mytest.php
<?php
defined('IN_PHPCMS') or exit('No permission resources');
class mytest {
public function __construct() {
$this->db=pc_base::load_model('test_model','',1);
}
public function init() {
$arr=$this->db->select();
//var_dump($arr);
include template('test','mytest','default');
}
}
?>
------------------------------------------------------------------------
六:创建模板文件mytest.html
文件路径:/mytest.html
=========================================================
=========================================================
=========================================================
路径:phpcms/modules/test/install/config.inc.php
<?php
defined('IN_PHPCMS') or exit('Access Denied');
defined('INSTALL') or exit('Access Denied');
$module = 'test';
$modulename = '测试';
$introduce = '测试模块';
$author = '个人测试';
$authorsite = 'http://www.phpcms.cn';
$authoremail = '';
?>
=========================================================
V9二次开发常用代码
- defined('IN_PHPCMS') or exit('No permission resources.');
- //第一步:获取模型缓存路径
- define('CACHE_MODEL_PATH',CACHE_PATH.'caches_model'.DIRECTORY_SEPARATOR.'caches_data'.DIRECTORY_SEPARATOR);
- pc_base::load_app_func('util','content');
- class index {
- private $db;
- function __construct() {
- //第二步:获取与数据库相关的配置信息,如:数据表前缀、数据库名、数据库用户名、数据库密码、数据库编码、数据库类型
- $this->db = pc_base::load_model('content_model');
- $this->_userid = param::get_cookie('_userid');
- $this->_username = param::get_cookie('_username');
- $this->_groupid = param::get_cookie('_groupid');
- }
- //首页
- public function init() {
- if(isset($_GET['siteid'])) {
- $siteid = intval($_GET['siteid']);
- } else {
- $siteid = 1;//模型情况下siteid为1
- }
- $siteid = $GLOBALS['siteid'] = max($siteid,1);
- define('SITEID', $siteid);
- $_userid = $this->_userid;
- $_username = $this->_username;
- $_groupid = $this->_groupid;
- //SEO
- $SEO = seo($siteid);
- $sitelist = getcache('sitelist','commons');
- $default_style = $sitelist[$siteid]['default_style'];
- $CATEGORYS = getcache('category_content_'.$siteid,'commons');
- include template('content','index',$default_style);
- }
- //内容页
- public function show() {
- $catid = intval($_GET['catid']);
- $id = intval($_GET['id']);
- if(!$catid || !$id) showmessage(L('information_does_not_exist'),'blank');
- $_userid = $this->_userid;
- $_username = $this->_username;
- $_groupid = $this->_groupid;
- $page = intval($_GET['page']);
- $page = max($page,1);
- $siteids = getcache('category_content','commons');//获取各栏目所对应的站点id
- $siteid = $siteids[$catid];//获取当前栏目所对应的站点id
- $CATEGORYS = getcache('category_content_'.$siteid,'commons');//获取当前站点下所有栏目的配置信息
- if(!isset($CATEGORYS[$catid]) || $CATEGORYS[$catid]['type']!=0) showmessage(L('information_does_not_exist'),'blank');
- $this->category = $CAT = $CATEGORYS[$catid];//获取当前站点下当前栏目的配置信息
- $this->category_setting = $CAT['setting'] = string2array($this->category['setting']);
- $siteid = $GLOBALS['siteid'] = $CAT['siteid'];//获取当前站点下当前栏目所对应的站点id值
- $MODEL = getcache('model','commons');//获取各个模型的配置信息
- $modelid = $CAT['modelid'];//获取当前站点下当前栏目所属的模型id,找到了模型,也就找到了要查询的模型表(数据表)
- //设置模型表:通过缓存文件获取modelid,然后再通过模型modelid获取对应的模型表及对应的模型附表
- $tablename = $this->db->table_name = $this->db->db_tablepre.$MODEL[$modelid]['tablename'];
- //设置模型附表
- $r = $this->db->get_one(array('id'=>$id));
- if(!$r || $r['status'] != 99) showmessage(L('info_does_not_exists'),'blank');
- //下面代码获取的是当前模型附表中数据
- $this->db->table_name = $tablename.'_data';
- //下面代码获取的是当前模型附表中数据
- $r2 = $this->db->get_one(array('id'=>$id));
- //将模型表数据和模型附表中数据合并在一起
- $rs = $r2 ? array_merge($r,$r2) : $r;
- //再次重新赋值,以数据库为准
- $catid = $CATEGORYS[$r['catid']]['catid'];
- $modelid = $CATEGORYS[$catid]['modelid'];
- require_once CACHE_MODEL_PATH.'content_output.class.php';
- $content_output = new content_output($modelid,$catid,$CATEGORYS);
- //将查询出来的数据经模型处理函数处理后放入$data数据中
- $data = $content_output->get($rs);
- //注意:这行代码非常之重要,主要用来将数组中各个元素转化为变量,数据元素的键名为变量名,数据元素的值为变量值
- extract($data);