一只小Songshu 2017-07-09 20:07:11 2644次浏览 0条评论 0 0 0

首先要做的就是创建文件目录啦
分为 config controllers libs models static views index.php

接下来就是代码环节
index.php
<?php

define('APP_PATH',realpath(dirname(__FILE__)));

define('DS',DIRECTORY_SEPARATOR);
define('STATIC',APP_PATH.DS.'static'.DS);
define('LIBS',APP_PATH.DS.'libs'.DS);
define('VIEWS',APP_PATH.DS.'views'.DS);

require(LIBS."AutoLoader.class.php"); // 实现自动加载
// 加载配置


if(new AutoLoader()){
	$router = \libs\Router::getInstance();
	$controller = $router->getCon();
	$action = $router->getAc();

	(new $controller)->$action();
}else{
	echo "MVC文件加载错误!";
}

config配置文件夹中
web.php
<?php
// 全局配置
return array(

'db'=>[
	'host'=>'127.0.0.1',
	'username'=>'root',
	'password'=>'root',
	'dbname'=>'yii',
	'tbPrefix'=>'',
	'pConnect'=>0,
],

);

controllers控制器文件
index.class.php
<?php

namespace controllers;
use libs\Controller;
use models\Joke;

class Index extends Controller {

public function index(){

	$model = new Joke();
	$data = $model->select("select * from users");
	//var_dump($data);
	$this->render('index',['data'=>$data]);
}

}

libs核心文件夹
AutoLoader.class.php
<?php

class AutoLoader{

public function __construct(){
	spl_autoload_register(array($this,"load"));
}

public function load($className){

	$className = str_replace("\\", DS, $className);
	$className.='.class.php';
	if(file_exists($className)){
		include $className;
	}else{
		echo "没有对应的控制器";
		exit;
	}
	
}

}
Configure.class.php
<?php

namespace libs;

class Configure {

public static $config;
public static function getConfigs(){
	self::$config = require APP_PATH.DS.'config'.DS."web.php";
	return self::$config;
}
public static function getDb(){
	return self::$config['db'];
}

}
Controller.class.php
<?php

namespace libs;

class Controller {

public function render($fileName,$data=[]){
	extract($data);
	include VIEWS.$fileName.'.php'; 
}

}
Model.class.php
<?php
namespace libs;

class Model{

private $host='127.0.0.1';
private $username='root';
private $password='';
private $dbname;
private $tablePrefix='';

private $tableName;
private $connect ;

public function __construct(){
	
	$config = Configure::getConfigs();
	$db_config = $config['db'];
	foreach($db_config as $key=>$v){
		$this->$key = $v;
	}
	$dns = "mysql:host={$this->host};dbname={$this->dbname};charset=utf8";
	try{
		$this->connect = new \PDO($dns,$this->username,$this->password);
	}catch (PDOException $e) {
		echo 'Connection failed: ' . $e->getMessage();
	}
}

public function insert($sql){
	return $this->connect->exec($sql);
	
}

public function save($sql){
	return $this->connect->exec($sql);

}

public function delete($sql){
	return $this->connect->exec($sql);
}

public function select($sql){
	$stm = $this->connect->query($sql);
	return $stm->fetchAll(\PDO::FETCH_ASSOC);
}

public function getTableName(){
	return $this->tableName;
}

public function setTableName($tableName){

	$this->tableName = $tableName;
}

}
Router.class.php
<?php
namespace libs;

class Router {

public static $instance = null;

private $controllerNamespace = '\controllers\\';
private $controller='Index';
private $action='index';

private function __construct(){
}

public static function getInstance(){
	if(!self::$instance){
		self::$instance = new self;
	}
	return self::$instance;
}

public function getCon(){
	if(isset($_GET['c']) && !empty($_GET['c'])){
		$this->controller = $_GET['c'];
	}
	return $this->controllerNamespace.$this->controller;
}

public function getAc(){
	if(isset($_GET['a']) && !empty($_GET['a'])){
		$this->action = $_GET['a'];
	}
	return $this->action;
}

}

接下来是models
模型层完全按照自己的需求来进行封装及正常调用就ok

static 也就是传说中的文件存放目录按照自己需求 可有可无

views 就是正常的视图层啦

    没有找到数据。
您需要登录后才可以评论。登录 | 立即注册