现在的位置: 首页 > 综合 > 正文

自己写的php简单的mvc框架

2016年12月07日 ⁄ 综合 ⁄ 共 2645字 ⁄ 字号 评论关闭

欢迎大家光顾我的淘宝店:点击进入

1  结构如图:

 

2 config.php文件内容

<?php
define('HOST', 'localhost');
define('NAME', 'root');
define('PASS', '');
define('DATABASE', 'test');
define('UT', 'utf8');
?>

 

 

3 controler.php内容

 

<?php
include_once('../templateEngine.php');
include_once('../model/userModel.php');

class controler{
   private $v=array();
   function __construct($v) {
     $this->v=$v;
   }
   function assign(){
   	  if(is_array($this->v)){
   	  	return $this->v;
   	  }
   	  return null;
   }	
}

$model=new userModel();

$records=$model->fetch(3);

foreach($records as $record){
	$c=new controler($record);
}

$file='../view/layout.phtml';

$template=new templateEngine($file,$c);


$template->display();
?>

4model.php的内容

 

<?php

class model{
     
     private $_connection;
     
     function __construct(){
        require_once('../config/config.php');
     	$this->connect();
     }
     
     function connect(){
     	$this->_connection=@mysql_connect(HOST,NAME,PASS)or die("数据库连接失败");
     	mysql_select_db(DATABASE,$this->_connection);
     	mysql_query("SET NAMES".UT);
     }
     function fetch($value,$key=null){
     	if(is_null($key)){
     		$key=$this->_primaryKey;
     	}
     	
     	$sql="select * from {$this->_tableName} where {$key}='{$value}' ";
     	$result=mysql_query($sql,$this->_connection);
     	
     	$row=array();
     	while($result=@mysql_fetch_array($result)){
     		$row[]=$result;
     	}
     	
     	return $row;
     }
     function update($keyArray){
     	$sql=" update {$this->_tableName} set ";
     	
     	$updates=array();
     	foreach($keyArray as $column=>$value){
     		$updates[]=" {$column}='{$value}'";
     	}
     	
     	$sql.=implode(',',$updates);
     	$sql.=" where {$this->_primaryKey}='{$keyArray[$this->_primaryKey]}'";
     	
     	mysql_query($sql,$this->_connection);
     }
}
?>

5 uerModel.php内容

 

<?php
include_once("model.php");

class userModel extends model{
	protected $_tableName='user';
	protected $_primaryKey='id';
	
	public function getUserByName($name){
		$result=$this->fetch($name,'name');
		
		return $result;
	}
}
?>

 

6 layout.php内容

 

 

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=gb2312" />
<title>{id}</title>
</head>
<body>
<!-- 导航菜单开始 -->
<div id="topmenu" style='height:24px; font-size:14pt'>
用户名 {name}
<br/>
密码 {pwd}
<br/>
提交时间 {posttime}
</div>
<!-- 导航菜单结束 -->
</body>
</html>

 

7templateEngine.php内容

 

<?php
class templateEngine{
	private $date=array();
    private $c;
	function __construct($s,controler $c) {
	  $this->c=$c;

	  if(file_exists($s)) $s = file_get_contents($s);//将整个文件读入成一个字符串
	  $this->find_var($s);
	  $this->data = explode('<', $s);
    }

    function display(){
    	//join是implode()函数的别名
    	eval('?>' . join('<', $this->data));

    }

    private function find_var(&$s) {
  	//用回调函数执行正则表达式的搜索和替换
	 $s = preg_replace_callback('/\{(\w+)\}/', array($this, 'var_callback'), $s);
    }
    protected function var_callback($r) {
    	
      return "<?php  \$value=\$this->c->assign(); echo isset(\$value)?\$value['$r[1]']:''; ?>";
   }
}
?>

 

8 测试结果

用户名 admin
密码 admin
提交时间 1331321331

 

 

 

抱歉!评论已关闭.