手把手编写PHP框架 深入了解MVC运行流程

2019-05-03 01:54:11王振洲

应该说,Sql.class.php 是框架的核心部分。为什么?因为通过它,我们创建了一个 SQL 抽象层,可以大大减少了数据库的编程工作。虽然 PDO 接口本来已经很简洁,但是抽象之后框架的可灵活性更高。 

3.8 视图View类 

视图类 View.class.php 内容如下:

 <?php
/**
 * 视图基类
 */
class View
{
  protected $variables = array();
  protected $_controller;
  protected $_action;

  function __construct($controller, $action)
  {
    $this->_controller = $controller;
    $this->_action = $action;
  }
 
  /** 分配变量 **/
  function assign($name, $value)
  {
    $this->variables[$name] = $value;
  }
 
  /** 渲染显示 **/
  function render()
  {
    extract($this->variables);
    $defaultHeader = APP_PATH . 'application/views/header.php';
    $defaultFooter = APP_PATH . 'application/views/footer.php';
    $controllerHeader = APP_PATH . 'application/views/' . $this->_controller . '/header.php';
    $controllerFooter = APP_PATH . 'application/views/' . $this->_controller . '/footer.php';
    
    // 页头文件
    if (file_exists($controllerHeader)) {
      include ($controllerHeader);
    } else {
      include ($defaultHeader);
    }

    // 页内容文件
    include (APP_PATH . 'application/views/' . $this->_controller . '/' . $this->_action . '.php');
    
    // 页脚文件
    if (file_exists($controllerFooter)) {
      include ($controllerFooter);
    } else {
      include ($defaultFooter);
    }
  }
}

 

这样我们的核心的PHP MVC框架就编写完成了,下面我们开始编写应用来测试框架功能。

4 应用

4.1 数据库部署

在 SQL 中新建一个 todo 数据库,使用下面的语句增加 item 数据表并插入2条记录:

CREATE DATABASE `todo` DEFAULT CHARACTER SET utf8 COLLATE utf8_general_ci;
USE `todo`;

CREATE TABLE `item` (
  `id` int(11) NOT NULL auto_increment,
  `item_name` varchar(255) NOT NULL,
  PRIMARY KEY (`id`)
) ENGINE=InnoDB AUTO_INCREMENT=1 DEFAULT CHARSET=utf8;
 
INSERT INTO `item` VALUES(1, 'Hello World.');
INSERT INTO `item` VALUES(2, 'Lets go!'); 

4.2 部署模型 

然后,我们还需要在 models 目录中创建一个 ItemModel.php 模型,内容如下:

 <?php

class ItemModel extends Model
{
  /* 业务逻辑层实现 */
}

模型内容为空。因为 Item 模型继承了 Model,所以它拥有 Model 的所有功能。

4.3 部署控制器 

在 controllers 目录下创建一个 ItemController.php 控制器,内容如下:

 <?php
 
class ItemController extends Controller
{
  // 首页方法,测试框架自定义DB查询
  public function index()
  {
    $items = (new ItemModel)->selectAll();

    $this->assign('title', '全部条目');
    $this->assign('items', $items);
  }
  
  // 添加记录,测试框架DB记录创建(Create)
  public function add()
  {
    $data['item_name'] = $_POST['value'];
    $count = (new ItemModel)->add($data);

    $this->assign('title', '添加成功');
    $this->assign('count', $count);
  }
  
  // 查看记录,测试框架DB记录读取(Read)
  public function view($id = null)
  {
    $item = (new ItemModel)->select($id);

    $this->assign('title', '正在查看' . $item['item_name']);
    $this->assign('item', $item);
  }
  
  // 更新记录,测试框架DB记录更新(Update)
  public function update()
  {
    $data = array('id' => $_POST['id'], 'item_name' => $_POST['value']);
    $count = (new ItemModel)->update($data['id'], $data);

    $this->assign('title', '修改成功');
    $this->assign('count', $count);
  }
  
  // 删除记录,测试框架DB记录删除(Delete)
  public function delete($id = null)
  {
    $count = (new ItemModel)->delete($id);

    $this->assign('title', '删除成功');
    $this->assign('count', $count);
  }
}

								 
			 
相关文章 大家在看