php 的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,我们继续,看第三句
$router = loader::load(”router”);
这句话也很简单,就是加载loader函数的静态函数load,下面我们来loader.php
<?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
<?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
<?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来测试下。
php 的mvc开发的更多相关文章
- ASP.NET MVC开发:Web项目开发必备知识点
最近加班加点完成一个Web项目,使用Asp.net MVC开发.很久以前接触的Asp.net开发还是Aspx形式,什么Razor引擎,什么MVC还是这次开发才明白,可以算是新手. 对新手而言,那进行A ...
- MVC开发模式下的用户角色权限控制
前提: MVC开发模式 大概思想: 1.在MVC开发模式下,每个功能都对应着不同的控制器或操作方法名(如修改密码功能可能对应着User/changepd),把每个功能对应的控制器名和操作方法名存到数据 ...
- ASP.Net MVC开发基础学习笔记:四、校验、AJAX与过滤器
一.校验 — 表单不是你想提想提就能提 1.1 DataAnnotations(数据注解) 位于 System.ComponentModel.DataAnnotations 命名空间中的特性指定对数据 ...
- MVC开发模式之Servlet+jsp+javaBean
Servlet+jsp+JavaBean组合开发是一种MVC开发模式,控制器Controller采用Servlet.模型Model采用JavaBean.视图View采用JSP. 1.Web开发的请求- ...
- Extjs MVC开发模式详解
Extjs MVC开发模式详解 在JS的开发过程中,大规模的JS脚本难以组织和维护,这一直是困扰前端开发人员的头等问题.Extjs为了解决这种问题,在Extjs 4.x版本中引入了MVC开发模式, ...
- ASP.Net MVC开发基础学习笔记(4):校验、AJAX与过滤器
一.校验 — 表单不是你想提想提就能提 1.1 DataAnnotations(数据注解) 位于 System.ComponentModel.DataAnnotations 命名空间中的特性指定对数据 ...
- 解析ASP.NET WebForm和Mvc开发的区别
因为以前主要是做WebFrom开发,对MVC开发并没有太深入的了解.自从来到创新工场的新团队后,用的技术都是自己以前没有接触过的,比如:MVC 和EF还有就是WCF,压力一直很大.在很多问题都是不清楚 ...
- 解析ASP.NET Mvc开发之查询数据实例
目录: 1)从明源动力到创新工场这一路走来 2)解析ASP.NET WebForm和Mvc开发的区别 ------------------------------------------------- ...
- 用Spring MVC开发简单的Web应用
这个例子是来自于Gary Mak等人写的Spring攻略(第二版)第八章Spring @MVC中的一个例子,在此以学习为目的进行记录. 问题:想用Spring MVC开发一个简单的Web应用, 学习这 ...
- MVC开发Markdown编辑器(2)
MVC开发Markdown编辑器(2) MVC Markdown 实时预览 我希望实现一个在线实时预览的Markdown编辑器,左边是编辑处,右边是实时预览界面. 准备工作 引入相关js和css 这里 ...
随机推荐
- MinGW开发工具的安装(还有visual-mingw)
MinGW是Minimalist GNU for Windows的缩写,是把linux下的GNU开发工具包移植到windows的项目之一.和Cygwin不一样的是,MinGW不提供linux的posi ...
- zoj 3820 Building Fire Stations(二分法+bfs)
题目链接:zoj 3820 Building Fire Stations 题目大意:给定一棵树.选取两个建立加油站,问说全部点距离加油站距离的最大值的最小值是多少,而且随意输出一种建立加油站的方式. ...
- SQL中的JOIN语法详解
参考以下两篇博客: 第一个是 sql语法:inner join on, left join on, right join on详细使用方法 讲了 inner join, left join, righ ...
- 用 Expression Blend 创建酷炫的 Button
原文:用 Expression Blend 创建酷炫的 Button 原文:Creating "Cool" Buttons with Expression Blend Author ...
- readline库的使用
接口十分简单,readline和addhistory: #include <stdlib.h> #include <stdio.h> #include <unistd.h ...
- WPF C# 多屏情况下,实现窗体显示到指定的屏幕内
原文:WPF C# 多屏情况下,实现窗体显示到指定的屏幕内 针对于一个程序,需要在两个显示屏上显示不同的窗体,(亦或N个显示屏N个窗体),可以使用如下的方式实现. 主要涉及到的:System.Wind ...
- MySQL TIMESTAMP(时间戳)详细解释
当你创建一个表假设表中有类型的字段TIMESTAMP,该字段默认情况下,语句生成: CREATE TABLE `test` ( `id` int(11) DEFAULT NULL, `ctime` t ...
- Ubuntu 14.04 64位字体美化(使用黑文泉驿)
Ubuntu 14.04安装和升级后,,斜体字体变得很难看,昨天,我得到一个晚上,最终,管理一个线索,这里整洁. 在线调研后,.一致的观点是,,使用开源字体库文泉驿理想的黑色字体效果,效果甚至没有丢失 ...
- 反编译 war 包成传统项目的方法
需求 项目老大让外包做了官网,不甚满意,想自己搞搞,遂叫我反编译他们发过来的 war 包. 方法 第一步:解压 war 包其实就是 zip 压缩包,用 zip 解压. 第二步:反编译 查看 war 包 ...
- 在Winform窗体中使用WPF控件(附源码)
原文:在Winform窗体中使用WPF控件(附源码) 今天是礼拜6,下雨,没有外出,闲暇就写一篇博文讲下如何在Winform中使用WPF控件.原有是我在百度上搜索相关信息无果,遂干脆动手自己实现. W ...