zoukankan      html  css  js  c++  java
  • php 搭建mvc框架

        这是一个最小化的Php mvc项目,以下通过对项目结构的分析,说明php中搭建mvc框架的最基本思路。

    1.MVC项目结构

    目录结构如下:

    /application      应用程序目录

             /model      模型类目录

             /view         视图类目录

             /controller   控制器类目录

    /framework      框架代码目录

             /Model.class.php     基础模型类

             /MySQLDB.class.php  数据库的操作类

    /index.php       入口文件

    项目文件结构图如下:

    2.framework (基础框架目录)

    2.1 MySQLDB.class.php

             mysql数据库操作类,构造方法中初始化了相关的参数,数据库连接等。同时在该类中并封装link(),selectDB(),query(),fetchAll(),fetchRow()等操作数据库的基本方法(后续可做进一步扩展)。

              /**

              * 构造方法

              *

              * @param $params array 需要初始化的数据集合!

              */

             public function __construct($params=array()) {

                       $this->host = isset($params['host']) ? $params['host'] : '127.0.0.1';

                       $this->port = isset($params['port']) ? $params['port'] : '3306';

                       $this->user = isset($params['user']) ? $params['user'] : 'root';

                       $this->pass = isset($params['pass']) ? $params['pass'] : '';

                       $this->charset = isset($params['charset']) ? $params['charset'] : 'utf8';

                       //如果默认数据库为空字符串,表示用户没有选择默认数据,意味着不需要执行use dbname!

                       $this->dbname = isset($params['dbname']) ? $params['dbname'] : '';

                       //连接数据库

                       $this->link();

                       //设置字符集

                       $this->setCharset();

                       //选择默认数据库

                       $this->selectDB();

             }

    2.2 Model.class.php

    基础模型类:所有模型类的基类,构造方法中初始化MySQLDB类的对象,并赋值给成员$db

    <?php

    /**

     * 基础模型类

     */

    class Model {

             protected $db;//初始化MySQLDB类的对象  

             public function __construct() {

                       //初始化数据库操作对象

                       $this->initDB();

             } 

             protected function initDB() {

                       //引入mysqldb类

                       require './framework/MySQLDB.class.php';

                       //实例化对象

                       $this->db = new MySQLDB(array('pass'=>'1234abcd', 'dbname'=>'itcast'));

             }

    }

    3.application (应用程序目录)

    3.1 Model 模型

    该文件夹包含以下模型类文件:

    MatchModel.class.php

    StudentModel.class.php

    以StudentModel为例,提供了getList()方法,返回所有学生数据。(方法可按需要扩展)

    由于继承自Model类,而基础的功能在MySQLDB类和Model类中已做了封装,因此这里的查询代码非常简洁。

    <?php

    require './framework/Model.class.php';

    class StudentModel extends Model {

             public function getList() {

                       return $this->db->fetchAll('select * from student');

             }

    }

    3.2 View  视图

    该文件夹包含以下视图文件:

    match_list.html

    student_list.html

    以student_list.html为例,将$list中的数据进行展示,php代码主要是展示数据的需要,不处理业务逻辑。

    这里的代码是作为模版(template),由控制器需要时进行调用的。

    <h1>学生列表</h1>

    <?php foreach($list as $row) :?>

             <?php echo $row['stu_name'];?>,

             <?php echo $row['gender'];?>,

             <br>

    <?php endForeach;?>

    3.3 Contrllor 控制器

    该文件夹包含以下控制器类文件:

    MatchController.class.php

    StudentController.class.php

    以StudentController为例,提供了相关Action的方法。如listAction()通过载入StudentModel.class.php文件,创建StudentModel类的对象来操作数据库,得到学生信息。然后调用学生列表视图模版,展示页面。

    控制器类中的方法是提供给入口文件(Index.php)调用的,因为所有的请求都是向单一的入口文件Index.php发起。

    <?php

    class StudentController {

             public function listAction() {

                       //载入学生模型

                       require './application/model/StudentModel.class.php';

                       $model_student = new StudentModel;

                       //得到学生列表数据

                       $list = $model_student->getList();

           

                       //调用一个学生列表视图模板,展示页面

                       require './application/view/student_list.html';

             }

    }

    4.Index.php (单一入口文件)

    单一入口文件:根据请求参数,创建控制器对象,执行所请求的Action对应的方法.

    <?php

    /**

     *入口文件

    */

    //根据请求c参数,确定当前的控制器类标识

    $c = isset($_GET['c']) ? $_GET['c'] : 'Match';

    //载入控制器类文件,得到控制器类对象

    $controller_name = $c . 'Controller';

    require './application/controller/' . $controller_name . '.class.php';

    $controller = new $controller_name;//可变类名

    //根据请求a参数,确定当前的Action标识

    $a = isset($_GET['a']) ? $_GET['a'] : 'list';

    //得到当前的方法

    $action_name = $a . 'Action';//拼凑方法名

    //调用

    $controller->$action_name();//可变方法名

    上一篇:php基础(三):php进行web开发

  • 相关阅读:
    AtCoder Beginner Contest 205
    Codeforces Round #725 (Div. 3)
    Educational Codeforces Round 110 (Rated for Div. 2)【A
    Codeforces Round #722 (Div. 2)
    AtCoder Beginner Contest 203(Sponsored by Panasonic)
    AISing Programming Contest 2021(AtCoder Beginner Contest 202)
    PTA 520 钻石争霸赛 2021
    Educational Codeforces Round 109 (Rated for Div. 2)【ABCD】
    AtCoder Beginner Contest 200 E
    Educational Codeforces Round 108 (Rated for Div. 2)【ABCD】
  • 原文地址:https://www.cnblogs.com/Extreme/p/3576896.html
Copyright © 2011-2022 走看看