您好,欢迎访问一九零五行业门户网

PHP MVC框架核心类

php mvc框架核心类 
现在我们举几个核心框架的例子演示:在framework/core下建立一个framework.class.php的文件。写入以下代码:
// framework/core/framework.class.php 
class framework { 
   public static function run() { 
       echo run(); 
   } 
<?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&rsquo;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 
<?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; 
    } 
}
其它类似信息

推荐信息