日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程语言 > php >内容正文

php

mvc 实例 php,来写一个简单的PHP MVC结构

發布時間:2024/9/30 php 27 豆豆
生活随笔 收集整理的這篇文章主要介紹了 mvc 实例 php,来写一个简单的PHP MVC结构 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

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

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

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

include("core/ini.php");

initializer::initialize();

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

dispatcher::dispatch($router);

?>

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

include("core/ini.php"); 我們來看core/ini.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增加的,就是當我們實例化一個函數的時候,如果本文件沒有,就會自動去加載文件。官方的解釋是:

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文件如下:

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

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 (empty(self::$loaded[$object])){

self::$loaded[$object]= new $object();

}

return self::$loaded[$object];

}

}

?>

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

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

router.php

class router

{

private $route;

private $controller;

private $action;

private $params;

public function __construct()

{

$path = array_keys($_GET);

if (!isset($path[0])){

if (!empty($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 (empty($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.tinoweb.cn/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

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

}

}

?>

然后你可以在瀏覽器中輸入http://localhost/index.php?user/register 或者 http://localhost/index.php?user/login來測試下。

總結

以上是生活随笔為你收集整理的mvc 实例 php,来写一个简单的PHP MVC结构的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。