skymvc开发手册之控制器ctrl概述
控制器文件命名规则:
url:index.php?m=guest
对应文件
source/index/guest.ctrl.php
一、control规范
以on开头的函数可以对外访问
非on开头的为内部私有方法
<?php
class guestControl extends skymvc{
public function __construct(){
parent::__construct();
}
/*初始化*/
public function onInit(){
}
/*对外访问的 要加on 首页*/
public function onDefault(){
echo "index";
}
/*列表页*/
public function onList(){
echo "list";
}
/*详细页*/
public function onShow(){
echo "show";
}
/*我的发布*/
public function onMy(){
echo "my";
}
/*编辑页*/
public function onAdd(){
echo "add";
}
/*保存*/
public function onSave(){
echo "save";
}
/*删除*/
public function onDelete(){
echo "delete";
}
/*
不对外访问的
*/
public function default(){
return 1;
}
}
?>二、使用模型
public function onDefault(){
$list=M("article")->select(array(
"where"=>" status=1 "
));
print_r($list);
}三、使用模板
模板文件规范
url:/index.php?m=index
模板:index/index.html
url:/index.php?m=index&a=show
模板:index/show.html
public function onDefault(){
$this->smarty->assign(array(
"title"=>"hello world"
));
$this->smarty->goAssign(array(
"json"=>"hello world"
));
$this->smarty->display("index/index.html");
}三、使用扩展类库
使用方法
$this->loadClass("checkcode");
$this->checkcode->setImg();或者
$this->loadClass("checkcode",false,false);
$checkcode=new checkcode();
$checkcode->setImg();public function onDefault(){
$this->loadClass("checkcode");
$this->checkcode->setImg();
}实例:
<?php
/**
*Author 雷日锦 362606856@qq.com
*控制器自动生成
*/
if(!defined("ROOT_PATH")) exit("die Access ");
class guestControl extends skymvc{
public function __construct(){
parent::__construct();
}
/*初始化*/
public function onInit(){
}
public function onDefault(){
$where="1";
$url="/index.php?m=guest&a=default";
$limit=20;
$start=get("per_page","i");
$option=array(
"start"=>intval(get_post('per_page')),
"limit"=>$limit,
"order"=>" id DESC",
"where"=>$where
);
$rscount=true;
$data=M("guest")->select($option,$rscount);
//分页
$pagelist=$this->pagelist($rscount,$limit,$url);
/
$this->smarty->goassign(
array(
"data"=>$data,
"pagelist"=>$pagelist,
"rscount"=>$rscount,
"url"=>$url,
"catlist"=>M("guest")->catlist()
)
);
$this->smarty->display("guest/index.html");
}
public function onShow(){
$id=get_post("id","i");
$data=M("guest")->selectRow(array("where"=>"id={$id}"));
$this->smarty->goassign(array(
"data"=>$data,
"catlist"=>M("guest")->catlist()
));
$this->smarty->display("guest/show.html");
}
public function onAdd(){
$id=get_post("id","i");
if($id){
$data=M("guest")->selectRow(array("where"=>"id={$id}"));
}
$this->smarty->goassign(array(
"data"=>$data,
"catlist"=>M("guest")->catlist()
));
$this->smarty->display("guest/add.html");
}
public function onSave(){
$id=get_post("id","i");
$data=M("guest")->postdata();
$data["dateline"]=time();
if($id){
$data["reply_time"]=time();
M("guest")->update($data,"id='$id'");
}else{
M("guest")->insert($data);
}
$this->goall("保存成功");
}
public function onDelete(){
$id=get_post('id',"i");
M("guest")->delete("id=".$id);
$this->goall("删除成功");
}
}
?>