模拟 yii2.0 实现简单的 MVC [ 2.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 就是正常的视图层啦
一只小Songshu
注册时间:2017-05-23
最后登录:2017-12-12
在线时长:2小时55分
最后登录:2017-12-12
在线时长:2小时55分
- 粉丝1
- 金钱70
- 威望10
- 积分190
热门源码
- 基于 Yii 2 + Bootstrap 3 搭建一套后台管理系统 CMF
- 整合完 yii2-rbac+yii2-admin+adminlte 等库的基础开发后台源码
- 适合初学者学习的一款通用的管理后台
- yii-goaop - 将 goaop 集成到 Yii,在 Yii 中优雅的面向切面编程
- yii-log-target - 监控系统异常且多渠道发送异常信息通知
- 店滴云1.3.0
- 面向对象的一小步:添加 ActiveRecord 的 Scope 功能
- Yii2 开源商城 FecShop
- 基于 Yii2 开发的多店铺商城系统,免费开源 + 适合二开
- leadshop - 基于 Yii2 开发的一款免费开源且支持商业使用的商城管理系统
共 0 条评论