今天研究了下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,我们继续,看第三句
至于什么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,我们继续,看第三句