手把手編寫PHP框架 深刻了解MVC運行流程

1 什麼是MVC php

MVC模式(Model-View-Controller)是軟件工程中的一種軟件架構模式,把軟件系統分爲三個基本部分:模型(Model)、視圖(View)和控制器(Controller)。 html

PHP中MVC模式也稱Web MVC,從上世紀70年代進化而來。MVC的目的是實現一種動態的程序設計,便於後續對程序的修改和擴展簡化,而且使程序某一部分的重複利用成爲可能。除 此以外,此模式經過對複雜度的簡化,使程序結構更加直觀。軟件系統經過對自身基本部份分離的同時,也賦予了各個基本部分應有的功能。 mysql

MVC各部分的職能:
 •模型Model – 管理大部分的業務邏輯和全部的數據庫邏輯。模型提供了鏈接和操做數據庫的抽象層。
 •控制器Controller - 負責響應用戶請求、準備數據,以及決定如何展現數據。
 •視圖View – 負責渲染數據,經過HTML方式呈現給用戶。 git

 

一個典型的Web MVC流程:
 1.Controller截獲用戶發出的請求;
 2.Controller調用Model完成狀態的讀寫操做;
 3.Controller把數據傳遞給View;
 4.View渲染最終結果並呈獻給用戶。 github

2 爲何要本身開發MVC框架 sql

網絡上有大量優秀的MVC框架可供使用,本教程並非爲了開發一個全面的、終極的MVC框架解決方案,而是將它看做是一個很好的從內部學習PHP的機會,在此過程當中,你將學習面向對象編程和MVC設計模式,並學習到開發中的一些注意事項。 數據庫

更重要的是,你能夠徹底控制你的框架,並將你的想法融入到你開發的框架中。雖然不必定是作好的,可是你能夠按照你的方式去開發功能和模塊。 編程

3 開始開發本身的MVC框架 設計模式

3.1 目錄準備 數組

在開始開發前,讓咱們先來把項目創建好,假設咱們創建的項目爲 todo,MVC的框架能夠命名爲 FastPHP,那麼接下來的第一步就是把目錄結構先設置好。

 

雖然在這個教程中不會使用到上面的全部的目錄,可是爲了之後程序的可拓展性,在一開始就把程序目錄設置好使很是必要的。下面就具體說說每一個目錄的做用:
 •application – 應用代碼
 •config – 程序配置或數據庫配置
 •fastphp - 框架核心目錄
 •public – 靜態文件
 •runtime - 臨時數據目錄
 •scripts – 命令行工具 

3.2 代碼規範

在目錄設置好之後,咱們接下來就要來規定一下代碼的規範:
 1.MySQL的表名需小寫,如:item,car
 2.模塊名(Models)需首字母大寫,,並在名稱後添加「Model」,如:ItemModel,CarModel
 3.控制器(Controllers)需首字母大寫,,並在名稱中添加「Controller」,如:ItemController,CarController
 4.視圖(Views)部署結構爲「控制器名/行爲名」,如:item/view.php,car/buy.php 

上述的一些規則是爲了能在程序中更好的進行互相的調用。接下來就開始真正的PHP MVC編程了。 

3.3 重定向 

將全部的數據請求都重定向 index.php 文件,在 todo 目錄下新建一個 .htaccess 文件,文件內容爲: 

?

1

2

3

4

5

6

7

8

9

10

<IfModule mod_rewrite.c>

  RewriteEngine On

 

  # 確保請求路徑不是一個文件名或目錄

  RewriteCond %{REQUEST_FILENAME} !-f

  RewriteCond %{REQUEST_FILENAME} !-d

 

  # 重定向全部請求到 index.php?url=PATHNAME

  RewriteRule ^(.*)$ index.php?url=$1 [PT,L]

</IfModule>

這樣作的主要緣由有:
 1.程序有一個單一的入口;
 2.除靜態程序,其餘全部程序都重定向到 index.php 上;
 3.能夠用來生成利於SEO的URL,想要更好的配置URL,後期可能會須要URL路由,這裏先不作介紹了。 

3.4 入口文件 

作完上面的操做,就應該知道咱們須要作什麼了,沒錯!在 public 目錄下添加 index.php 文件,文件內容爲:

?

1

2

3

4

5

6

7

8

9

10

11

12

13

<?php

 

// 應用目錄爲當前目錄

define('APP_PATH', __DIR__.'/');

 

// 開啓調試模式

define('APP_DEBUG', true);

 

// 網站根URL

define('APP_URL', 'http://localhost/fastphp');

 

// 加載框架

require './fastphp/FastPHP.php';

注意,上面的PHP代碼中,並無添加PHP結束符號」?>」,這麼作的主要緣由是,對於只有 PHP 代碼的文件,結束標誌(「?>」)最好不存在,PHP自身並不須要結束符號,不添加結束符號能夠很大程度上防止末尾被添加額外的注入內容,讓程序更加安全。 

3.5 配置文件和主請求 

index.php 中,咱們對 fastphp  文件夾下的 FastPHP.php 發起了請求,那麼 FastPHP.php 這個啓動文件中到底會包含哪些內容呢?

?

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

<?php

 

// 初始化常量

defined('FRAME_PATH') or define('FRAME_PATH', __DIR__.'/');

defined('APP_PATH') or define('APP_PATH', dirname($_SERVER['SCRIPT_FILENAME']).'/');

defined('APP_DEBUG') or define('APP_DEBUG', false);

defined('CONFIG_PATH') or define('CONFIG_PATH', APP_PATH.'config/');

defined('RUNTIME_PATH') or define('RUNTIME_PATH', APP_PATH.'runtime/');

 

// 包含配置文件

require APP_PATH . 'config/config.php';

 

//包含核心框架類

require FRAME_PATH . 'Core.php';

 

// 實例化核心類

$fast = new Core;

$fast->run();

以上文件都其實能夠直接在 index.php 文件中包含,常量也能夠直接在 index.php 中定義,咱們這麼作的緣由是爲了在後期管理和拓展中更加的方便,因此把須要在一開始的時候就加載運行的程序統一放到一個單獨的文件中引用。

先來看看config文件下的 config .php 文件,該文件的主要做用是設置一些程序的配置項及數據庫鏈接等,主要內容爲: 

?

1

2

3

4

5

6

7

8

<?php

  

/** 變量配置 **/

  

define('DB_NAME', 'todo');

define('DB_USER', 'root');

define('DB_PASSWORD', 'root');

define('DB_HOST', 'localhost');

應該說 config.php 涉及到的內容並很少,不過是一些基礎數據庫的設置,再來看看 fastphp下的共用框架入口文件 Core.php 應該怎麼寫。
 

?

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

33

34

35

36

37

38

39

40

41

42

43

44

45

46

47

48

49

50

51

52

53

54

55

56

57

58

59

60

61

62

63

64

65

66

67

68

69

70

71

72

73

74

75

76

77

78

79

80

81

82

83

84

85

86

87

88

89

90

91

92

93

94

95

96

97

98

99

100

101

102

103

104

105

106

107

108

109

110

111

112

113

114

115

116

117

118

119

120

121

<?php

/**

 * FastPHP核心框架

 */

class Core

{

  // 運行程序

  function run()

  {

    spl_autoload_register(array($this, 'loadClass'));

    $this->setReporting();

    $this->removeMagicQuotes();

    $this->unregisterGlobals();

    $this->Route();

  }

 

  // 路由處理

  function Route()

  {

    $controllerName = 'Index';

    $action = 'index';

 

    if (!empty($_GET['url'])) {

      $url = $_GET['url'];

      $urlArray = explode('/', $url);

       

      // 獲取控制器名

      $controllerName = ucfirst($urlArray[0]);

       

      // 獲取動做名

      array_shift($urlArray);

      $action = empty($urlArray[0]) ? 'index' : $urlArray[0];

       

      //獲取URL參數

      array_shift($urlArray);

      $queryString = empty($urlArray) ? array() : $urlArray;

    }

 

    // 數據爲空的處理

    $queryString = empty($queryString) ? array() : $queryString;

 

    // 實例化控制器

    $controller = $controllerName . 'Controller';

    $dispatch = new $controller($controllerName, $action);

 

    // 若是控制器存和動做存在,這調用並傳入URL參數

    if ((int)method_exists($controller, $action)) {

      call_user_func_array(array($dispatch, $action), $queryString);

    } else {

      exit($controller . "控制器不存在");

    }

  }

 

  // 檢測開發環境

  function setReporting()

  {

    if (APP_DEBUG === true) {

      error_reporting(E_ALL);

      ini_set('display_errors','On');

    } else {

      error_reporting(E_ALL);

      ini_set('display_errors','Off');

      ini_set('log_errors', 'On');

      ini_set('error_log', RUNTIME_PATH. 'logs/error.log');

    }

  }

 

  // 刪除敏感字符

  function stripSlashesDeep($value)

  {

    $value = is_array($value) ? array_map('stripSlashesDeep', $value) : stripslashes($value);

    return $value;

  }

 

  // 檢測敏感字符並刪除

  function removeMagicQuotes()

  {

    if ( get_magic_quotes_gpc()) {

      $_GET = stripSlashesDeep($_GET );

      $_POST = stripSlashesDeep($_POST );

      $_COOKIE = stripSlashesDeep($_COOKIE);

      $_SESSION = stripSlashesDeep($_SESSION);

    }

  }

 

  // 檢測自定義全局變量(register globals)並移除

  function unregisterGlobals()

  {

    if (ini_get('register_globals')) {

      $array = array('_SESSION', '_POST', '_GET', '_COOKIE', '_REQUEST', '_SERVER', '_ENV', '_FILES');

      foreach ($array as $value) {

        foreach ($GLOBALS[$value] as $key => $var) {

          if ($var === $GLOBALS[$key]) {

            unset($GLOBALS[$key]);

          }

        }

      }

    }

  }

 

  // 自動加載控制器和模型類

  static function loadClass($class)

  {

    $frameworks = FRAME_PATH . $class . '.class.php';

    $controllers = APP_PATH . 'application/controllers/' . $class . '.class.php';

    $models = APP_PATH . 'application/models/' . $class . '.class.php';

 

    if (file_exists($frameworks)) {

      // 加載框架核心類

      include $frameworks;

    } elseif (file_exists($controllers)) {

      // 加載應用控制器類

      include $controllers;

    } elseif (file_exists($models)) {

      //加載應用模型類

      include $models;

    } else {

      /* 錯誤代碼 */

    }

  }

}

下面重點講解主請求方法 callHook(),首先咱們想看看咱們的 URL 會這樣:
yoursite.com/controllerName/actionName/queryString 

callHook()的做用就是,從全局變量  G ET[ ′ url ′ ]變量中獲取URL,並將其分割成三部分: GET[′url′]變量中獲取URL,並將其分割成三部分:controller、action和 action和queryString。 

例如,URL連接爲:todo.com/item/view/1/first-item,那麼
 •$controller 就是:item
 •$action 就是:view
 •查詢字符串Query String就是:array(1, first-item) 

分割完成後,會實例化一個新的控制器:$controller.'Controller'(其中「.」是連字符),並調用其方法 $action。 

3.6 控制器/Controller基類 

接下來的操做就是在 fastphp 中創建程序所需的基類,包括控制器、模型和視圖的基類。 

新建控制器基類爲 Controller.class.php,控制器的主要功能就是總調度,具體具體內容以下:
 

?

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

<?php

/**

 * 控制器基類

 */

class Controller

{

  protected $_controller;

  protected $_action;

  protected $_view;

  

  // 構造函數,初始化屬性,並實例化對應模型

  function __construct($controller, $action)

  {

    $this->_controller = $controller;

    $this->_action = $action;

    $this->_view = new View($controller, $action);

  }

 

  // 分配變量

  function assign($name, $value)

  {

    $this->_view->assign($name, $value);

  }

 

  // 渲染視圖

  function __destruct()

  {

    $this->_view->render();

  }

}

 

Controller 類實現全部控制器、模型和視圖(View類)的通訊。在執行析構函數時,咱們能夠調用 render() 來顯示視圖(view)文件。

3.7 模型Model基類

新建模型基類爲 Model.class.php,模型基類 Model.class.php 代碼以下:

?

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

<?php

 

class Model extends Sql

{

  protected $_model;

  protected $_table;

  

  function __construct()

  {

    // 鏈接數據庫

    $this->connect(DB_HOST, DB_USER, DB_PASSWORD, DB_NAME);

     

    // 獲取模型名稱

    $this->_model = get_class($this);

    $this->_model = rtrim($this->_model, 'Model');

     

    // 數據庫表名與類名一致

    $this->_table = strtolower($this->_model);

  }

  

  function __destruct()

  {

  }

}

 考慮到模型須要對數據庫進行處理,因此單獨創建一個數據庫基類 Sql.class.php,模型基類繼承 Sql.class.php,代碼以下:

?

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

33

34

35

36

37

38

39

40

41

42

43

44

45

46

47

48

49

50

51

52

53

54

55

56

57

58

59

60

61

62

63

64

65

66

67

68

69

70

71

72

73

74

75

76

77

78

79

80

81

82

83

84

85

86

87

88

89

90

91

92

93

94

95

96

97

98

99

100

<?php

 

class Sql

{

  protected $_dbHandle;

  protected $_result;

 

  // 鏈接數據庫

  public function connect($host, $user, $pass, $dbname)

  {

    try {

      $dsn = sprintf("mysql:host=%s;dbname=%s;charset=utf8", $host, $dbname);

      $this->_dbHandle = new PDO($dsn, $user, $pass, array(PDO::ATTR_DEFAULT_FETCH_MODE => PDO::FETCH_ASSOC));

    } catch (PDOException $e) {

      exit('錯誤: ' . $e->getMessage());

    }

  }

 

  // 查詢全部

  public function selectAll()

  {

    $sql = sprintf("select * from `%s`", $this->_table);

    $sth = $this->_dbHandle->prepare($sql);

    $sth->execute();

 

    return $sth->fetchAll();

  }

 

  // 根據條件 (id) 查詢

  public function select($id)

  {

    $sql = sprintf("select * from `%s` where `id` = '%s'", $this->_table, $id);

    $sth = $this->_dbHandle->prepare($sql);

    $sth->execute();

     

    return $sth->fetch();

  }

 

  // 根據條件 (id) 刪除

  public function delete($id)

  {

    $sql = sprintf("delete from `%s` where `id` = '%s'", $this->_table, $id);

    $sth = $this->_dbHandle->prepare($sql);

    $sth->execute();

 

    return $sth->rowCount();

  }

 

  // 自定義SQL查詢,返回影響的行數

  public function query($sql)

  {

    $sth = $this->_dbHandle->prepare($sql);

    $sth->execute();

 

    return $sth->rowCount();

  }

 

  // 新增數據

  public function add($data)

  {

    $sql = sprintf("insert into `%s` %s", $this->_table, $this->formatInsert($data));

 

    return $this->query($sql);

  }

 

  // 修改數據

  public function update($id, $data)

  {

    $sql = sprintf("update `%s` set %s where `id` = '%s'", $this->_table, $this->formatUpdate($data), $id);

 

    return $this->query($sql);

  }

 

  // 將數組轉換成插入格式的sql語句

  private function formatInsert($data)

  {

    $fields = array();

    $values = array();

    foreach ($data as $key => $value) {

      $fields[] = sprintf("`%s`", $key);

      $values[] = sprintf("'%s'", $value);

    }

 

    $field = implode(',', $fields);

    $value = implode(',', $values);

 

    return sprintf("(%s) values (%s)", $field, $value);

  }

 

  // 將數組轉換成更新格式的sql語句

  private function formatUpdate($data)

  {

    $fields = array();

    foreach ($data as $key => $value) {

      $fields[] = sprintf("`%s` = '%s'", $key, $value);

    }

 

    return implode(',', $fields);

  }

}

應該說,Sql.class.php 是框架的核心部分。爲何?由於經過它,咱們建立了一個 SQL 抽象層,能夠大大減小了數據庫的編程工做。雖然 PDO 接口原本已經很簡潔,可是抽象以後框架的可靈活性更高。 

3.8 視圖View類 

視圖類 View.class.php 內容以下:

?

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

33

34

35

36

37

38

39

40

41

42

43

44

45

46

47

48

49

<?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條記錄:

?

1

2

3

4

5

6

7

8

9

10

11

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 模型,內容以下:

?

1

2

3

4

5

6

<?php

 

class ItemModel extends Model

{

  /* 業務邏輯層實現 */

}

模型內容爲空。由於 Item 模型繼承了 Model,因此它擁有 Model 的全部功能。

4.3 部署控制器 

controllers 目錄下建立一個 ItemController.php 控制器,內容以下:

?

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

33

34

35

36

37

38

39

40

41

42

43

44

45

46

47

48

49

50

51

<?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);

  }

}

4.4 部署視圖 

views 目錄下新建 header.php 和 footer.php 兩個頁頭頁腳模板,內容以下。 

header.php,內容:

?

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

33

34

35

36

37

38

39

40

41

42

43

44

45

46

47

48

49

50

51

52

53

54

55

<html>

<head>

  <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />

  <title><?php echo $title ?></title>

  <style>

    .item {

      width:400px;

    }

  

    input {

      color:#222222;

      font-family:georgia,times;

      font-size:24px;

      font-weight:normal;

      line-height:1.2em;

      color:black;

    }

  

    a {

      color:blue;

      font-family:georgia,times;

      font-size:20px;

      font-weight:normal;

      line-height:1.2em;

      text-decoration:none;

     }

  

    a:hover {

      text-decoration:underline;

    }

 

    h1 {

      color:#000000;

      font-size:41px;

      letter-spacing:-2px;

      line-height:1em;

      font-family:helvetica,arial,sans-serif;

      border-bottom:1px dotted #cccccc;

    }

  

    h2 {

      color:#000000;

      font-size:34px;

      letter-spacing:-2px;

      line-height:1em;

      font-family:helvetica,arial,sans-serif;

    }

  </style>

</head>

<body>

  <h1><?php echo $title ?></h1>

  

footer.php,內容:

 </body>

</html>

而後,在 views/item 建立如下幾個視圖文件。 

index.php,瀏覽數據庫內 item 表的全部記錄,內容:

?

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

<form action="<?php echo APP_URL ?>/item/add" method="post">

  <input type="text" value="點擊添加" onclick="this.value=''" name="value">

  <input type="submit" value="添加">

</form>

<br/><br/>

 

<?php $number = 0?>

  

<?php foreach ($items as $item): ?>

  <a class="big" href="<?php echo APP_URL ?>/item/view/<?php echo $item['id'] ?>" title="點擊修改">

    <span class="item">

      <?php echo ++$number ?>

      <?php echo $item['item_name'] ?>

    </span>

  </a>

  ----

  <a class="big" href="<?php echo APP_URL ?>/item/delete/<?php echo $item['id']?>">刪除</a>

<br/>

<?php endforeach ?>

add.php,添加記錄,內容:
 <a class="big" href="<?php echo APP_URL ?>/item/index">成功添加<?php echo $count ?>條記錄,點擊返回</a> 

view.php,查看單條記錄,內容:

?

1

2

3

4

5

6

7

<form action="<?php echo APP_URL ?>/item/update" method="post">

  <input type="text" name="value" value="<?php echo $item['item_name'] ?>">

  <input type="hidden" name="id" value="<?php echo $item['id'] ?>">

  <input type="submit" value="修改">

</form>

 

<a class="big" href="<?php echo APP_URL ?>/item/index">返回</a>

update.php,更改記錄,內容:
 <a class="big" href="<?php echo APP_URL ?>/item/index">成功修改<?php echo $count ?>項,點擊返回</a> 

delete.php,刪除記錄,內容:
 <a href="<?php echo APP_URL ?>/item/index">成功刪除<?php echo $count ?>項,點擊返回</a> 

4.5 應用測試 

這樣,在瀏覽器中訪問 todo 程序:http://localhost/todo/item/index/,就能夠看到效果了。 

以上代碼已經所有發佈到 github 上,關鍵部分加航了註釋,倉庫地址:https://github.com/yeszao/fastphp,歡迎克隆、提交。

要設計更好的MVC,或使用得更加規範,請看MVC架構的職責劃分原則》 。

相關文章
相關標籤/搜索