一个简单的php mvc框架 的 代码说明,contrllor action 处理后,填充数据进模板,输出HTML。
例如:
当Model_Route 解析出 controller name = index action name = index 时,就执行:
$obj = new Controller_index();
$obj->action_index();
然后这个action_index()就该做些什么就做些什么,
最后做完了,要显示网页了,就调用 $this->render()
$this->render() 这个method 是在它继承的 上级 class library/Action.php 里面的
<?php
class Controller_index extends Action {
//每个controller class都要继承的上级class Action,它的功能主要是将数据填入模板,然后渲染,将HTML输出浏览器
//它位于/project/library/action.php 代码在此之后
public function init(){
//我是init里面的代码,在这里,可以初始化cookie,当前访客信息,
//或者权限检查
//这是在所有action之前运行的method
}
public function action_index(){
$this->title = $title = '我是title';
$list = Db::instance()->fetchAll("select * from table1 where status = 1 limit 0,20");
$this->list = $list;
$this->render();
}
}
下面就是位于/project/library/action.php 的class Action,负责将数据填入模板,然后渲染,将HTML输出浏览器的父class。
继承了它以后,可以用它的method:$this->render();渲染,将HTML输出浏览器
<?php
class Action {
private $_hash;
public function __call($method,$args){
return _Exception("action:{$method} not found");
}
public function __get($name){
if(isset($this->_hash["{$name}"])){
return $this->_hash["{$name}"];
}
}
public function __set($name,$value){
return $this->_hash["{$name}"] = $value;
}
public function __construct(){
return $this->init();
}
public function init(){
return null;
}
public function render_get($tpl){
@ob_end_clean();
ob_start();
require APPLICATION_PATH . "/view/{$tpl}.php";
return ob_get_clean();
}
public function render($tpl = 'template'){//一般是调用这个method //如果有mobile端的,可以传入参数换掉主模板template,默认template.php
return require APPLICATION_PATH . "/view/{$tpl}.php";
}
public function _render($tpl = null){
//但特殊情况也可以直接调用这个method,直接传入子模板相对view目录的路径到该method即可(不包含后缀.php)
//例如在controller action 里面写:$this->_render('msg'),调用子模板 view/msg.php
global $_SGLOBAL;
if($tpl == null){
$tpl = APPLICATION_PATH . "/view/" . $_SGLOBAL['controller'] . "/" . $_SGLOBAL['action'] . ".php";
}
else{
$tpl = APPLICATION_PATH . "/view/" . $tpl . ".php";
}
if(file_exists($tpl)){
require $tpl;
}
else{
return _Exception("template: {$tpl} not found");
}
}
public function _GET($key){
if(isset($_GET["{$key}"])){
return $_GET["{$key}"];
}
else{
return null;
}
}
}
/project/applaction_www/view/template.php
这是主模板,里面的 <?php $this->_render($this->_tpl);?> 自动嵌入 uri解析出来的controller action 对应的action子模板
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html>
<meta http-equiv="content-type" content="text/html; charset=utf-8" />
<head>
<title><?php $title = $this->title;echo !empty($title) ? $title : '我是标题'?></title>
<link href="/css/all.css" rel="stylesheet" />
<script src="/js/jquery-1.5.1.min.js" ></script>
<script src="/js/jquery.form.js" ></script>
</head>
<body>
<div class="mainbody">
<!--这里是模板html-->
<div class="content">
<?php $this->_render($this->_tpl);?>
</div>
<!--这里是模板html-->
</div>
</body>
</html>
以上是主模板,template.php ,如果是移动手机端网页不一样,可以在template.php同目录建一个template_mobile.php(名字怎么起,随你了),然后调用时传入参数即可,这样:$this->render('template_mobile'),很方便的。
如果contrller name = index action name = index
那么就自动嵌入子模板 /project/application_www/view/index/index.php
<div>
<?php
$list = $this->list;
if(!empty($list)):?>
<?php foreach($list as $k=>$v):?>
<div> <?php echo $v['username'];?></div>
<?endforeach;?>
<?php endif;?>
</div>
子模板就这么简单,它自动嵌入主模板template.php后就是完整的html输出了。
=============================
当然也可以不用模板,比如输出json,根本不用模板,就不用$this->render()了,直接echo。