PHP MVC架構核心類

來源:互聯網
上載者:User
PHP MVC架構核心類
現在我們舉幾個核心架構的例子示範:在framework/core下建立一個Framework.class.php的檔案。寫入以下代碼:

// framework/core/Framework.class.php
class Framework {
public static function run() {
echo "run()";
}
兄弟連教育www.lampbrother.net在這個示範中建立了一個靜態方法run(),現在讓我們通過入口檔案index.php測試一下:

require "framework/core/Framework.class.php";
Framework::run();
你可以在你的瀏覽器裡訪問index.php看到結果。通常這個靜態方法被命名為run()或者bootstrap()。在這個方法中,我們要做3件最主要的事情:

class Framework {
public static function run() {
// echo "run()";
self::init();
self::autoload();
self::dispatch();
}
private static function init() {
}
private static function autoload() {
}
private static function dispatch() {
}
}
初始化
init()方法:

// Initialization

private static function init() {

// Define path constants

define("DS", DIRECTORY_SEPARATOR);

define("ROOT", getcwd() . DS);

define("APP_PATH", ROOT . 'application' . DS);

define("FRAMEWORK_PATH", ROOT . "framework" . DS);

define("PUBLIC_PATH", ROOT . "public" . DS);

define("CONFIG_PATH", APP_PATH . "config" . DS);

define("CONTROLLER_PATH", APP_PATH . "controllers" . DS);

define("MODEL_PATH", APP_PATH . "models" . DS);

define("VIEW_PATH", APP_PATH . "views" . DS);

define("CORE_PATH", FRAMEWORK_PATH . "core" . DS);

define('DB_PATH', FRAMEWORK_PATH . "database" . DS);

define("LIB_PATH", FRAMEWORK_PATH . "libraries" . DS);

define("HELPER_PATH", FRAMEWORK_PATH . "helpers" . DS);

define("UPLOAD_PATH", PUBLIC_PATH . "uploads" . DS);

// Define platform, controller, action, for example:

// index.php?p=admin&c=Goods&a=add

define("PLATFORM", isset($_REQUEST['p']) ? $_REQUEST['p'] : 'home');

define("CONTROLLER", isset($_REQUEST['c']) ? $_REQUEST['c'] : 'Index');

define("ACTION", isset($_REQUEST['a']) ? $_REQUEST['a'] : 'index');

define("CURR_CONTROLLER_PATH", CONTROLLER_PATH . PLATFORM . DS);

define("CURR_VIEW_PATH", VIEW_PATH . PLATFORM . DS);

// Load core classes

require CORE_PATH . "Controller.class.php";

require CORE_PATH . "Loader.class.php";

require DB_PATH . "Mysql.class.php";

require CORE_PATH . "Model.class.php";


// Load configuration file
$GLOBALS['config'] = include CONFIG_PATH . "config.php";
// Start session
session_start();
}
在注釋中你可以看到每一步的目的。

自動載入
在項目中,我們不想在指令碼中想使用一個類的時候手動的去include或者require載入,這就是為什麼PHP MVC架構都有自動載入的功能。例如,在symfony中,如果你想要載入lib下的類,它將會被自動引入。很神奇是吧?現在讓我們在自己的架構中加入自動載入的功能。

這裡我們要用的PHP中的內建函數spl_autoload_register:

// Autoloading

private static function autoload(){

spl_autoload_register(array(__CLASS__,'load'));
}

// Define a custom load method
private static function load($classname){

// Here simply autoload app’s controller and model classes

if (substr($classname, -10) == "Controller"){

// Controller

require_once CURR_CONTROLLER_PATH . "$classname.class.php";

} elseif (substr($classname, -5) == "Model"){

// Model

require_once MODEL_PATH . "$classname.class.php";
}
}
每一個架構都有自己的命名規則,我們的也不例外。對於一個控制器類,它需要被命名成類似xxxController.class.php,對於一個模型類,需要被命名成xxModel.class.php。為什麼在使用一個架構的時候你需要遵守它的命名規則呢?自動載入就是一條原因。

路由/分發
// Routing and dispatching

private static function dispatch(){

// Instantiate the controller class and call its action method

$controller_name = CONTROLLER . "Controller";

$action_name = ACTION . "Action";

$controller = new $controller_name;

$controller->$action_name();

}
在這步中,index.php將會分發請求到對應的Controller::Aciton()方法中。

基礎Controller類
通常在架構的核心類中都有一個基礎的控制器。在symfony中,被稱為sfAction;在iOS中,被稱為UIViewController。在這裡我們命名為Controller,在framework/core下建立Controller.class.php
// Base Controller
class Controller{
// Base Controller has a property called $loader, it is an instance of Loader class(introduced later)
protected $loader;
public function __construct(){
$this->loader = new Loader();
}
public function redirect($url,$message,$wait = 0){
if ($wait == 0){
header("Location:$url");
} else {
include CURR_VIEW_PATH . "message.html";
}
exit;
}
}
基礎控制器有一個變數$loader,它是Loader類的執行個體化(後面介紹)。準確的說,$this->loader是一個變數指向了被執行個體化的Load類。在這裡我不過多的討論,但是這的確是一個非常關鍵的概念。我遇到過一些PHP開發人員相信在這個語句之後:
$this->loader = new Loader();
$this->load是一個對象。不,它只是一個引用。這是從Java中開始使用的,在Java之前,在C++和Objective C中被稱為指標。引用是個封裝的指標類型。比如,在iOS(O-C)中,我們建立了一個對象:
UIButton *btn = [UIButton alloc] init];
載入類
在framework.class.php中,我們已經封裝好了應用的控制器和模型的自動載入。但是如何自動載入在framework目錄中的類呢?現在我們可以建立一個Loader類,它會載入framework目錄中的類和函數。當我們載入framework類時,只需要調用這個Loader類中的方法即可。
class Loader{
// Load library classes
public function library($lib){
include LIB_PATH . "$lib.class.php";
}
// loader helper functions. Naming conversion is xxx_helper.php;
public function helper($helper){
include HELPER_PATH . "{$helper}_helper.php";
}
}
  • 聯繫我們

    該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

    如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

    A Free Trial That Lets You Build Big!

    Start building with 50+ products and up to 12 months usage for Elastic Compute Service

    • Sales Support

      1 on 1 presale consultation

    • After-Sales Support

      24/7 Technical Support 6 Free Tickets per Quarter Faster Response

    • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.