天天看點

MVC前提之單一入口+例子

單一入口概述 set_include_path

單一入口的應用程式就是說用一個檔案處理所有的http請求,例如不管是清單頁還是文章頁,都是從浏覽器通路index.php檔案,這個檔案就是這個應用程式的單一入口。

打個比方,大家都要上wc,都是男生進一個門,女生進一個門,這兩個門就是wc的兩個入口。而現在去一個公園裡面的wc,外面還有一個門,不管男女都從最外面的門進入,交了錢以後才進入裡面的男廁所門或女廁所門,而這個最外面的門就是這個wc的單一入口。

實作方式 

很簡單,可以在通路index.php時限上一個特定的參數。例如index.php?action=list就是通路清單頁,而index.php?action=single則通路文章頁。

實作代碼:

$action=$_get['action']==''?'index':$_get['action'];//從url中取出action參數,如果沒有提供action參數,就設定一個預設的'index'作為參數

include('files/'.$action.'.php');//根據$action參數調用不同的代碼檔案,進而滿足單一入口實作對應的不同的功能。

MVC前提之單一入口+例子

<?php  

$admincp_actions_founder = array ('templates', 'db', 'founder', 'postsplit', 'threadsplit');  

$action = $_get ['action'] == '' ? 'index' : $_get ['action'];  

$operation = $_get ['operation'] == '' ? 'index': $_get ['operation'];  

if(empty($action)){  

    header('location:index.php?action=login');  

}elseif(in_array($action, $admincp_actions_founder)) {  

    include('files/'.$action.'.php');  

}  

?>   

$action.php

MVC前提之單一入口+例子

if(!$operation) {  

// do query from db and show page  

} elseif($operation == 'list') {  

} elseif($operation == 'remove') {  

} elseif($operation == 'add') {  

?>  

面向對象的調用方法

MVC前提之單一入口+例子

<?php    

class autoloader {    

    public static $loader;    

    public static function init() {    

        if (self::$loader == null)    

            self::$loader = new self ();    

        return self::$loader;    

    }    

    public function __construct() {    

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

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

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

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

    public function library($class) {    

        set_include_path ( get_include_path () . path_separator . '/lib/' );    

        spl_autoload_extensions ( '.library.php' );    

        spl_autoload ( $class );    

    public function controller($class) {    

        $class = preg_replace ( '/_controller$/ui', '', $class );    

        set_include_path ( get_include_path () . path_separator . '/controller/' );    

        spl_autoload_extensions ( '.controller.php' );    

    public function model($class) {    

        $class = preg_replace ( '/_model$/ui', '', $class );    

        set_include_path ( get_include_path () . path_separator . '/model/' );    

        spl_autoload_extensions ( '.model.php' );    

    public function helper($class) {    

        $class = preg_replace ( '/_helper$/ui', '', $class );    

        set_include_path ( get_include_path () . path_separator . '/helper/' );    

        spl_autoload_extensions ( '.helper.php' );    

}    

//call    

autoloader::init ();    

?>    

單一入口應用程式的優勢

單一入口應用程式的所有http請求都是通過index.php接收并轉發到功能代碼去的,是以在index.php裡面就能完成許多實際工作 ,如autoload,init,cache,常用的公共方法等。

由于所有的http請求都由index.php接收,是以可以進行集中的安全性檢查 ,如果不是單一入口,那麼開發者就必須記得在每一個檔案的開始加上安全性檢查代碼(當然,安全性檢查可以寫到另一個檔案中,隻需要include就可以了。)

與安全性檢查類似。在入口裡,我們還可以對url參數和post進行必要的檢查和特殊字元過濾、記錄日志、通路統計等等各種可以集中處理的任務。

這樣就可以看出,由于這些工作都被集中到了index.php來完成,可以減輕我們維護其他功能代碼的難度。 

單一入口應用程式的缺點 

任何事情都有兩面性,單一入口應用程式也不例外。由于所有http請求都是針對index.php,是以程式的url看起來确實不那麼美觀,特别是對搜尋引擎來說很不友好。要解決這個問題,可以采用url重寫、pathinfo等方式 ,但也可以在前台頁面不使用單一入口方式,而是保持多個檔案入口。或者兩者混用。

對于單入口(通路網站必需首先通過某一檔案,一般都是index.php來實作其它功能的項目)程式來說:常見的url大都為:http://www.nostop.org/index.php?controller=posts&action=index

說實話這樣的url很難看,最重要的是搜尋引擎不認它為正常的url,這很可怕。如何實作一個好看的且與搜尋引擎能攀上親的url呢。

看這個url:http://www.nostop.org/index.php/posts/index/

很新奇的url創意,這也是cakephp架構的過程中體驗出來的僞靜态,此url相對于彼url來說:易記,美觀,最主要的是搜尋引擎雖不認它為老爸,起碼不會把它劃為黑名單。

解釋一下上面的url,"posts":一般稱其為controller(控制器),由它來決定加載哪個處理檔案;

"index":一般稱其為 action(操作),由它來決定要進行什麼操作。

是以/posts/index/顧名思義就是posts的首頁了。如果後面需要傳遞參數可以直接加在 action後面。

如下面這個url:http://www.nostop.org/index.php/posts/category/1/。列出posts的分類id為1下的所有内容。

php中的一些架構,zend framework,thinkphp,fleaphp,qeephp,還有cakephp 等都是單入口模式,它們都采用了統一的入口,可以根據不同的需要,分前台入口背景入口或者其他操作權限入口 ,這樣的設計模式優點很明顯,比如。權限控制,url重寫,結合mvc清晰地目錄結構。這些都是單入口模式所帶來的便利,當然這樣的也會帶來執行效率的問。