Home  >  Article  >  Backend Development  >  开发自个儿的PHP MVC框架(一)

开发自个儿的PHP MVC框架(一)

WBOY
WBOYOriginal
2016-06-13 12:29:03661browse

开发自己的PHP MVC框架(一)

这个教程可以使大家掌握用mvc模式开发php应用的基本概念。此教程分为三个部分,现在这篇是第一部分。

现在市面上有很多流行的框架供大家使用,但是我们也可以自己动手开发一个mvc框架,采用mvc模式可以大大减少我们开发应用的时间,并且能够更好的组织项目源代码,而且其中的某些模块还可在其它项目中使用。现在我要教大家写一个简单的mvc框架。由于这个项目很简单,轻量,所以可能并不是最佳实践,也不具备安全性,还需要大家在实际应用中完善。

所用技术:php,面向对象开发方法。

开始

首先在网站根目录下建立三个文件夹

  • models
  • views
  • controllers

然后在根目录下新建一个文件:

  • index.php

现在项目结构应该像这样

§ 网站根目录

  § index.php

  § models/

  § views/

  § controllers/


index.php是整个web应用的入口点,所有的用户请求都会经过它。我们会写一些代码来把用户请求分派到相应的控制器中,这些控制器存放在controllers文件夹里。之后,我们就可以用下面的方式来实现页面跳转:

  • http://你的域名.com/index.php?page1
  • http://你的域名.com/index.php?page2
  • http://你的域名.com/index.php?page3

设置前端控制器index.php

首先在index.php中定义网站根目录和网站域名,以便在整个应用中访问。

[php] view plain copy
  1.   
  2. //应用的根目录就是index.php的父目录  
  3. define("SERVER_ROOT", dirname(__FILE__));  
  4.   
  5. //你的域名.comm 是你的服务器域名  
  6. define('SITE_ROOT' , 'http://你的域名.com');  

定义了网站根目录后,在任何php文件中,都能很方便的引用其它目录的php文件,因为index.php是入口文件,这样就能够在整个应用中访问在它之中定义的这些变量。

设置路由器router.php(转发用户请求到相应控制器)

在controllers目录下新建一个文件,名字为“router.php",这个文件用来处理所有页面请求。想像一下你家里的路由器,它负责把internet路由到家中的每个电脑。router.php文件将会获取传入到index.php的页面请求,然后把请求分派给不同的控制器(controllers)。

route.php中的代码:

[php] view plain copy
  1.   
  2. //获取所有请求  
  3. $request = $_SERVER['QUERY_STRING'];  

这句代码会获取传入到应用中的请求参数。QUERY_STRING就是”?“后面的所有字符串。

  • http://你的域名.com/index.php?page1
上面的地址会在代码中得到”page1&action=login“,为了把page1和后面的参数分开,我们需要在route.php中继续加入下列代码:
[php] view plain copy
  1. //解析$request变量,得到用户请求的页面(page1)和其它GET变量(&分隔的变量)如一个请求http://你的域名.com/index.php?page1&article=buildawebsite,则被解析为array("page1", "article=buildawebsite")  
  2. $parsed = explode('&' , $request);  
  3.   
  4. //用户请求的页面,如上面的page1,为$parsed第一个变量,shift之后,数组为array("article=buildawebsite")  
  5. $page = array_shift($parsed);  
  6.   
  7. //剩下的为GET变量,把它们解析出来  
  8. $getVars = array();  
  9. foreach ($parsed as $argument)  
  10. {  
  11.     //用"="分隔字符串,左边为变量,右边为值  
  12.     list($variable , $value) = split('=' , $argument);  
  13.     $getVars[$variable] = $value;  
  14. }  
  15.   
  16. //这是测试语句,一会儿会删除  
  17. print "The page your requested is '$page'";  
  18. print '
    '
    ;  
  19. $vars = print_r($getVars, TRUE);  
  20. print "The following GET vars were passed to the page:
    "
    .$vars."";      
  21.       
现在我们需要在index.php中引入route.php
[php] view plain copy
  1. /** 
  2.  * 定义文档路径 
  3.  */  
  4. define("SERVER_ROOT", dirname(__FILE__));  
  5. define('SITE_ROOT' , 'http://你的域名.com');  
  6. /**  
  7.  * 引入router.php  
  8.  */  
  9.  require_once(SERVER_ROOT . '/controllers/' . 'router.php');  
  10. ?>  
如果顺利的话,你可以打开浏览器输入:

  • http://你的域名.com/index.php?news&article=howtobuildaframework
我们会看到如下输出
[html] view plain copy
  1. The page you requested is 'news'  
  2.     The following GET vars were passed to the page:  
  3.   
  4.     Array  
  5.     (  
  6.         [article] => howtobuildaframework  
  7.     )  
  8.       

如果没有上述输出,请检查你的服务器配置是否正确,并检查代码是否有错误。
现在来让我们添加一个页面到我们的网站里,这样就可以让router.php来产生一个页面,而不是直接输出上面的信息。

创建一个控制器(controller)
在controllers文件夹里新建一个文件名为“news.php",定义如下的类:
[php] view plain copy
  1. /** 
  2.  * 这个文件处理文章的查询,并提供文章 
  3.  */  
  4. class News_Controller  
  5. {  
  6.     /** 
  7.      * $template变量会保存与此控制器相关的"view(视图)"的文件名,不包括.php后缀  
  8.      */  
  9.     public $template = 'news';  
  10.   
  11.     /** 
  12.      * 此方法为route.php默认调用 
  13.      *  
  14.      * @param array $getVars 传入到index.php的GET变量数组 
  15.      */  
  16.     public function main(array $getVars)  
  17.     {  
  18.         //测试代码,以后会删除  
  19.         print "We are in news!";  
  20.         print '
    '
    ;  
  21.         $vars = print_r($getVars, TRUE);  
  22.         print   
  23.         (  
  24.             "The following GET vars were passed to this controller:" .  
  25.             "
    "
    .$vars.""  
  26.         );  
  27.     }  
  28. }  

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn