今天研究了下PHP MVC結構,所以決定自己寫個簡單的MVC,以待以后有空再豐富。
至于什么MVC結構,其實就是三個Model,Contraller,View單詞的簡稱,Model,主要任務就是把數據庫或者其他文件系統的數據按 照我們需要的方式讀取出來,View,主要負責頁面的,把數據以html的形式顯示給用戶,Controller,主要負責業務邏輯,根據用戶的 Request進行請求的分配,比如說顯示登陸界面,就需要調用一個控制器userController的方法loginAction來顯示.
下面我們用PHP來創建一個簡單的MVC結構系統。
首先創建單點入口,即bootstrap文件index.php,作為整個MVC系統的唯一入口。什么是單點入口呢?所謂單點入口就是整個應用程序只有一 個入口,所有的實現都通過這個入口來轉發。為什么要做到單點入口呢?單點入口有幾大好處:第一、一些系統全局處理的變量,類,方法都可以在這里進行處理。 比如說你要對數據進行初步的過濾,你要模擬session處理,你要定義一些全局變量,甚至你要注冊一些對象或者變量到注冊器里面。第二、程序的架構更加 清晰明了。當然好處還有很多的。:)
- <?php
- include("core/ini.php");
- initializer::initialize();
- $router = loader::load("router");
- dispatcher::dispatch($router);
- ?>
這個文件就只有4句,我們現在一句句來分析。
include(”core/ini.php”);
我們來看core/ini.php
- <?php
- set_include_path(get_include_path() . PATH_SEPARATOR . "core/main");
- //set_include_path — Sets the include_path configuration option
- function __autoload($object){
- require_once("{$object}.php");
- }
- ?>
這個文件首先設置了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文件如下:
- <?php
- class initializer
- {
- public static function initialize() {
- set_include_path(get_include_path().PATH_SEPARATOR . "core/main");
- set_include_path(get_include_path().PATH_SEPARATOR . "core/main/cache");
- set_include_path(get_include_path().PATH_SEPARATOR . "core/helpers");
- set_include_path(get_include_path().PATH_SEPARATOR . "core/libraries");
- set_include_path(get_include_path().PATH_SEPARATOR . "app/controllers");
- set_include_path(get_include_path().PATH_SEPARATOR."app/models");
- set_include_path(get_include_path().PATH_SEPARATOR."app/views");
- //include_once("core/config/config.php");
- }
- }
- ?>
這個函數很簡單,就只定義了一個靜態函數,initialize函數,這個函數就是設置include_path,這樣,以后如果包含文件,或者__autoload,就會去這些目錄下查找。
OK,我們繼續,看第三句:
$router = loader::load(”router”);
這句話也很簡單,就是加載loader函數的靜態函數load,下面我們來loader.php:
- <?php
- class loader
- {
- private static $loaded = array();
- public static function load($object){
- $valid = array( "library",
- "view",
- "model",
- "helper",
- "router",
- "config",
- "hook",
- "cache",
- "db");
- if (!in_array($object,$valid)){
- throw new Exception("Not a valid object '{$object}' to load");
- }
- if (emptyempty(self::$loaded[$object])){
- self::$loaded[$object]= new $object();
- }
- return self::$loaded[$object];
- }
- }
- ?>
這個文件就是去加載對象,因為以后我們可能會豐富這個MVC系統,會有model,helper,config等等的組件,如果加載的組件不在有效 的范圍內,我們拋出一個異常,如果在的話,我們實例化一個對象,其實這里用了單件設計模式,也就是這個對象其實就只能是一個實例化對象,如果沒有實例化,創建一個,如果存在的,則不實例化.
好,因為我們現在要加載的是router組件,所以我們看下router.php文件,這個文件的作用就是映射URL,對URL進行解析.
router.php:
- <?php
- class router
- {
- private $route;
- private $controller;
- private $action;
- private $params;
- public function __construct()
- {
- $path = array_keys($_GET);
- if (!isset($path[0])){
- if (!emptyempty($default_controller))
- $path[0] = $default_controller;
- else
- $path[0] = "index";
- }
- $route= $path[0];
- $this->route = $route;
- $routeParts = split( "/",$route);
- $this->controller=$routeParts[0];
- $this->action=isset($routeParts[1])? $routeParts[1]:"base";
- array_shift($routeParts);
- array_shift($routeParts);
- $this->params=$routeParts;
- }
- public function getAction() {
- if (emptyempty($this->action)) $this->action="main";
- return $this->action;
- }
- public function getController() {
- return $this->controller;
- }
- public function getParams() {
- return $this->params;
- }
- }
- ?>
我們可以看到,首先我們是拿到$_GET,用戶Request的URL,然后從URL里我們解析出Controller和Action,以及Params
比如我們的地址是http://www.companysz.com/user/profile/id/3
那么從上面的地址,我們可以拿到controller是user,action似乎profile,參數是id以及3
OK我們看最后一句,就是
dispatcher::dispatch($router);
這句話的意思很明了,就是拿到URL解析的結果,然后通過dispatcher來分發controlloer及action來Response給用戶
好,我們來看下dispatcher.php文件:
- <?
- class dispatcher
- {
- public static function dispatch($router)
- {
- global $app;
- ob_start();
- $start = microtime(true);
- $controller = $router->getController();
- $action = $router->getAction();
- $params = $router->getParams();
- $controllerfile = "app/controllers/{$controller}.php";
- if (file_exists($controllerfile)){
- require_once($controllerfile);
- $app = new $controller();
- $app->setParams($params);
- $app->$action();
- if (isset($start)) echo "
- Tota1l time for dispatching is : ".(microtime(true)-$start)." seconds.
- ";
- $output = ob_get_clean();
- echo $output;
- }else{
- throw new Exception("Controller not found");
- }
- }
- }
- ?>
這個類很明顯,就是拿到$router來,尋找文件中的controller和action來回應用戶的請求.
OK,我們一個簡單的,MVC結構,就這樣,當然這里還不能算是一個很完整的MVC,因為這里還沒有涉及到View和Model,有空我再這里豐富.
我們來寫個Controller文件來測試下上面的這個系統,我們在app/controllers/下創建一個user.php文件,user.php:
- <?php
- class user
- {
- function base()
- {
- }
- public function login()
- {
- echo 'login html page';
- }
- public function register()
- {
- echo 'register html page';
- }
- public function setParams($params){
- var_dump($params);
- }
- }
- ?>
新聞熱點
疑難解答
圖片精選