国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 開發 > ThinkPHP > 正文

PHP實現MVC開發:一個簡單的MVC

2024-09-09 15:20:00
字體:
來源:轉載
供稿:網友

今天研究了下PHP MVC結構,所以決定自己寫個簡單的MVC,以待以后有空再豐富。

至于什么MVC結構,其實就是三個Model,Contraller,View單詞的簡稱,Model,主要任務就是把數據庫或者其他文件系統的數據按 照我們需要的方式讀取出來,View,主要負責頁面的,把數據以html的形式顯示給用戶,Controller,主要負責業務邏輯,根據用戶的 Request進行請求的分配,比如說顯示登陸界面,就需要調用一個控制器userController的方法loginAction來顯示.

下面我們用PHP來創建一個簡單的MVC結構系統。

首先創建單點入口,即bootstrap文件index.php,作為整個MVC系統的唯一入口。什么是單點入口呢?所謂單點入口就是整個應用程序只有一 個入口,所有的實現都通過這個入口來轉發。為什么要做到單點入口呢?單點入口有幾大好處:第一、一些系統全局處理的變量,類,方法都可以在這里進行處理。 比如說你要對數據進行初步的過濾,你要模擬session處理,你要定義一些全局變量,甚至你要注冊一些對象或者變量到注冊器里面。第二、程序的架構更加 清晰明了。當然好處還有很多的。:)

  1. <?php 
  2. include("core/ini.php"); 
  3. initializer::initialize(); 
  4. $router = loader::load("router"); 
  5. dispatcher::dispatch($router); 
  6. ?> 

這個文件就只有4句,我們現在一句句來分析。

include(”core/ini.php”);

我們來看core/ini.php

  1. <?php 
  2. set_include_path(get_include_path() . PATH_SEPARATOR . "core/main"); 
  3. //set_include_path — Sets the include_path configuration option 
  4. function __autoload($object){ 
  5. require_once("{$object}.php"); 
  6. ?> 

這個文件首先設置了include_path,也就是我們如果要找包含的文件,告訴系統在這個目錄下查找。其實我們定義__autoload()方法,這個方法是在PHP5增加的,就是當我們實例化一個函數的時候,如果本文件沒有,就會自動去加載文件。官方的解釋是:

Many developers writing object-oriented applications create one PHP source file per-class definition. One of the biggest annoyances is having to write a long list of needed includes at the beginning of each script (one for each class).

In PHP 5, this is no longer necessary. You may define an __autoload function which is automatically called in case you are trying to use a class/interface which hasn’t been defined yet. By calling this function the scripting engine is given a last chance to load the class before PHP fails with an error.

接下來我們看下面一句:

initializer::initialize();

這就話就是調用initializer類的一個靜態函數initialize,因為我們在ini.php,設置了include_path,以及定義了__autoload,所以程序會自動在core/main目錄查找initializer.php.

initializer.php文件如下:

  1. <?php 
  2. class initializer 
  3. public static function initialize() { 
  4. set_include_path(get_include_path().PATH_SEPARATOR . "core/main"); 
  5. set_include_path(get_include_path().PATH_SEPARATOR . "core/main/cache"); 
  6. set_include_path(get_include_path().PATH_SEPARATOR . "core/helpers"); 
  7. set_include_path(get_include_path().PATH_SEPARATOR . "core/libraries"); 
  8. set_include_path(get_include_path().PATH_SEPARATOR . "app/controllers"); 
  9. set_include_path(get_include_path().PATH_SEPARATOR."app/models"); 
  10. set_include_path(get_include_path().PATH_SEPARATOR."app/views"); 
  11. //include_once("core/config/config.php"); 
  12. ?> 

這個函數很簡單,就只定義了一個靜態函數,initialize函數,這個函數就是設置include_path,這樣,以后如果包含文件,或者__autoload,就會去這些目錄下查找。

OK,我們繼續,看第三句:

$router = loader::load(”router”);

這句話也很簡單,就是加載loader函數的靜態函數load,下面我們來loader.php:

  1. <?php 
  2. class loader 
  3. private static $loaded = array(); 
  4. public static function load($object){ 
  5. $valid = array"library"
  6. "view"
  7. "model"
  8. "helper"
  9. "router"
  10. "config"
  11. "hook"
  12. "cache"
  13. "db"); 
  14. if (!in_array($object,$valid)){ 
  15. throw new Exception("Not a valid object '{$object}' to load"); 
  16. if (emptyempty(self::$loaded[$object])){ 
  17. self::$loaded[$object]= new $object(); 
  18. return self::$loaded[$object]; 
  19. ?> 

這個文件就是去加載對象,因為以后我們可能會豐富這個MVC系統,會有model,helper,config等等的組件,如果加載的組件不在有效 的范圍內,我們拋出一個異常,如果在的話,我們實例化一個對象,其實這里用了單件設計模式,也就是這個對象其實就只能是一個實例化對象,如果沒有實例化,創建一個,如果存在的,則不實例化.

好,因為我們現在要加載的是router組件,所以我們看下router.php文件,這個文件的作用就是映射URL,對URL進行解析.

router.php:

  1. <?php 
  2. class router 
  3. private $route
  4. private $controller
  5. private $action
  6. private $params
  7. public function __construct() 
  8. $path = array_keys($_GET); 
  9. if (!isset($path[0])){ 
  10. if (!emptyempty($default_controller)) 
  11. $path[0] = $default_controller
  12. else 
  13. $path[0] = "index"
  14. $route$path[0]; 
  15. $this->route = $route
  16. $routeParts = split( "/",$route); 
  17. $this->controller=$routeParts[0]; 
  18. $this->action=isset($routeParts[1])? $routeParts[1]:"base"
  19. array_shift($routeParts); 
  20. array_shift($routeParts); 
  21. $this->params=$routeParts
  22. public function getAction() { 
  23. if (emptyempty($this->action)) $this->action="main"
  24. return $this->action; 
  25. public function getController() { 
  26. return $this->controller; 
  27. public function getParams() { 
  28. return $this->params; 
  29. ?> 

我們可以看到,首先我們是拿到$_GET,用戶Request的URL,然后從URL里我們解析出Controller和Action,以及Params

比如我們的地址是http://www.survivalescaperooms.com/user/profile/id/3

那么從上面的地址,我們可以拿到controller是user,action似乎profile,參數是id以及3

OK我們看最后一句,就是

dispatcher::dispatch($router);

這句話的意思很明了,就是拿到URL解析的結果,然后通過dispatcher來分發controlloer及action來Response給用戶

好,我們來看下dispatcher.php文件:

  1. <? 
  2. class dispatcher 
  3. public static function dispatch($router
  4. global $app
  5. ob_start(); 
  6. $start = microtime(true); 
  7. $controller = $router->getController(); 
  8. $action = $router->getAction(); 
  9. $params = $router->getParams(); 
  10. $controllerfile = "app/controllers/{$controller}.php"
  11. if (file_exists($controllerfile)){ 
  12. require_once($controllerfile); 
  13. $app = new $controller(); 
  14. $app->setParams($params); 
  15. $app->$action(); 
  16. if (isset($start)) echo " 
  17.  
  18. Tota1l time for dispatching is : ".(microtime(true)-$start)." seconds. 
  19.  
  20. "; 
  21. $output = ob_get_clean(); 
  22. echo $output
  23. }else
  24. throw new Exception("Controller not found"); 
  25. ?> 

這個類很明顯,就是拿到$router來,尋找文件中的controller和action來回應用戶的請求.

OK,我們一個簡單的,MVC結構,就這樣,當然這里還不能算是一個很完整的MVC,因為這里還沒有涉及到View和Model,有空我再這里豐富.

我們來寫個Controller文件來測試下上面的這個系統,我們在app/controllers/下創建一個user.php文件,user.php:

  1. <?php 
  2. class user 
  3. function base() 
  4. public function login() 
  5. echo 'login html page'
  6. public function register() 
  7. echo 'register html page'
  8. public function setParams($params){ 
  9. var_dump($params); 
  10. ?> 

發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表

圖片精選

主站蜘蛛池模板: 常山县| 铜川市| 赤城县| 谢通门县| 泰来县| 南陵县| 晋州市| 平远县| 荃湾区| 义乌市| 敖汉旗| 山西省| 海南省| 长葛市| 舞钢市| 青岛市| 孝感市| 贵阳市| 新建县| 张掖市| 合山市| 辉南县| 区。| 莱阳市| 富民县| 新郑市| 屏东市| 沙田区| 洛川县| 中宁县| 大兴区| 延川县| 陆良县| 临清市| 平塘县| 汶上县| 平潭县| 静乐县| 古浪县| 耒阳市| 湖州市|