MVC前提之单一入口+例子

简介:

单一入口概述 set_include_path
单一入口的应用程序就是说用一个文件处理所有的HTTP请求,例如不管是列表页还是文章页,都是从浏览器访问index.php文件,这个文件就是这个应用程序的单一入口。

打个比方,大家都要上WC,都是男生进一个门,女生进一个门,这两个门就是WC的两个入口。而现在去一个公园里面的WC,外面还有一个门,不管男女都从最外面的门进入,交了钱以后才进入里面的男厕所门或女厕所门,而这个最外面的门就是这个WC的单一入口。

实现方式 
很简单,可以在访问index.php时限上一个特定的参数。例如index.php?action=list就是访问列表页,而index.php?action=single则访问文章页。

实现代码:
$action=$_GET['action']==''?'index':$_GET['action'];//从url中取出action参数,如果没有提供action参数,就设置一个默认的'index'作为参数
include('files/'.$action.'.php');//根据$action参数调用不同的代码文件,从而满足单一入口实现对应的不同的功能。

Index.php代码   收藏代码
  1. <?php  
  2. $admincp_actions_founder = array ('templates''db''founder''postsplit''threadsplit');  
  3.   
  4. $action = $_GET ['action'] == '' ? 'index' : $_GET ['action'];  
  5. $operation = $_GET ['operation'] == '' ? 'index': $_GET ['operation'];  
  6. if(empty($action)){  
  7.     header('location:index.php?action=login');  
  8. }elseif(in_array($action, $admincp_actions_founder)) {  
  9.     include('files/'.$action.'.php');  
  10. }  
  11. ?>   

$action.php

Java代码   收藏代码
  1. <?php  
  2. if(!$operation) {  
  3. // do query from db and show page  
  4. } elseif($operation == 'list') {  
  5. // do query from db and show page  
  6. } elseif($operation == 'remove') {  
  7. // do query from db and show page  
  8. } elseif($operation == 'add') {  
  9. // do query from db and show page  
  10. }  
  11. ?>  

面向对象的调用方法

Action.php代码   收藏代码
  1. <?php    
  2. class autoloader {    
  3.     public static $loader;    
  4.         
  5.     public static function init() {    
  6.         if (self::$loader == NULL)    
  7.             self::$loader = new self ();    
  8.             
  9.         return self::$loader;    
  10.     }    
  11.         
  12.     public function __construct() {    
  13.         spl_autoload_register ( array ($this, 'model' ) );    
  14.         spl_autoload_register ( array ($this, 'helper' ) );    
  15.         spl_autoload_register ( array ($this, 'controller' ) );    
  16.         spl_autoload_register ( array ($this, 'library' ) );    
  17.     }    
  18.         
  19.     public function library($class) {    
  20.         set_include_path ( get_include_path () . PATH_SEPARATOR . '/lib/' );    
  21.         spl_autoload_extensions ( '.library.php' );    
  22.         spl_autoload ( $class );    
  23.     }    
  24.         
  25.     public function controller($class) {    
  26.         $class = preg_replace ( '/_controller$/ui''', $class );    
  27.             
  28.         set_include_path ( get_include_path () . PATH_SEPARATOR . '/controller/' );    
  29.         spl_autoload_extensions ( '.controller.php' );    
  30.         spl_autoload ( $class );    
  31.     }    
  32.         
  33.     public function model($class) {    
  34.         $class = preg_replace ( '/_model$/ui''', $class );    
  35.             
  36.         set_include_path ( get_include_path () . PATH_SEPARATOR . '/model/' );    
  37.         spl_autoload_extensions ( '.model.php' );    
  38.         spl_autoload ( $class );    
  39.     }    
  40.         
  41.     public function helper($class) {    
  42.         $class = preg_replace ( '/_helper$/ui''', $class );    
  43.             
  44.         set_include_path ( get_include_path () . PATH_SEPARATOR . '/helper/' );    
  45.         spl_autoload_extensions ( '.helper.php' );    
  46.         spl_autoload ( $class );    
  47.     }    
  48.    
  49. }    
  50.    
  51. //call    
  52. autoloader::init ();    
  53. ?>    

单一入口应用程序的优势

单一入口应用程序的所有http请求都是通过index.php接收并转发到功能代码去的,所以在index.php里面就能完成许多实际工作 ,如autoload,init,cache,常用的公共方法等。

由于所有的http请求都由index.php接收,所以可以进行集中的安全性检查 ,如果不是单一入口,那么开发者就必须记得在每一个文件的开始加上安全性检查代码(当然,安全性检查可以写到另一个文件中,只需要include就可以了。)

与安全性检查类似。在入口里,我们还可以对url参数和post进行必要的检查和特殊字符过滤、记录日志、访问统计等等各种可以集中处理的任务。
这样就可以看出,由于这些工作都被集中到了index.php来完成,可以减轻我们维护其他功能代码的难度。 

单一入口应用程序的缺点 
任何事情都有两面性,单一入口应用程序也不例外。由于所有http请求都是针对index.php,所以程序的url看起来确实不那么美观,特别是对搜索引擎来说很不友好。要解决这个问题,可以采用url重写、PATHINFO等方式 ,但也可以在前台页面不使用单一入口方式,而是保持多个文件入口。或者两者混用。

对于单入口(访问网站必需首先通过某一文件,一般都是index.php来实现其它功能的项目)程序来说:常见的Url大都为:http://www.nostop.org/index.php?controller=posts&action=index
说实话这样的URL很难看,最重要的是搜索引擎不认它为正常的URL,这很可怕。如何实现一个好看的且与搜索引擎能攀上亲的URL呢。

 

看这个URL:http://www.nostop.org/index.php/posts/index/
很新奇的URL创意,这也是CakePHP框架的过程中体验出来的伪静态,此URL相对于彼URL来说:易记,美观,最主要的是搜索引擎虽不认它为老爸,起码不会把它划为黑名单。

解释一下上面的URL,"posts":一般称其为Controller(控制器),由它来决定加载哪个处理文件;

"index":一般称其为 Action(操作),由它来决定要进行什么操作。

所以/posts/index/顾名思义就是posts的主页了。如果后面需要传递参数可以直接加在 Action后面。

如下面这个URL:http://www.nostop.org/index.php/posts/category/1/。列出posts的分类ID为1下的所有内容。

PHP中的一些框架,zend framework,thinkphp,fleaphp,qeephp,还有cakephp 等都是单入口模式,它们都采用了统一的入口,可以根据不同的需要,分前台入口后台入口或者其他操作权限入口 ,这样的设计模式优点很明显,比如。权限控制,url重写,结合MVC清晰地目录结构。这些都是单入口模式所带来的便利,当然这样的也会带来执行效率的问。

相关文章
|
Web App开发 前端开发 Java
Spring MVC 上下文(ApplicationContext)初始化入口
Spring 常用上下文容器有哪些 ApplicationContext ClassPathXmlApplicationContext ApplicationContext context = new ClassPathXmlApplicationContext(applicationContext.
1372 0
|
3月前
|
开发框架 前端开发 .NET
ASP.NET CORE 3.1 MVC“指定的网络名不再可用\企图在不存在的网络连接上进行操作”的问题解决过程
ASP.NET CORE 3.1 MVC“指定的网络名不再可用\企图在不存在的网络连接上进行操作”的问题解决过程
38 0
|
8月前
|
存储 开发框架 前端开发
[回馈]ASP.NET Core MVC开发实战之商城系统(五)
经过一段时间的准备,新的一期【ASP.NET Core MVC开发实战之商城系统】已经开始,在之前的文章中,讲解了商城系统的整体功能设计,页面布局设计,环境搭建,系统配置,及首页【商品类型,banner条,友情链接,降价促销,新品爆款】,商品列表页面,商品详情等功能的开发,今天继续讲解购物车功能开发,仅供学习分享使用,如有不足之处,还请指正。
114 0
|
9月前
|
开发框架 前端开发 .NET
[回馈]ASP.NET Core MVC开发实战之商城系统(三)
[回馈]ASP.NET Core MVC开发实战之商城系统(三)
67 0
|
9月前
|
开发框架 前端开发 .NET
[回馈]ASP.NET Core MVC开发实战之商城系统(一)
[回馈]ASP.NET Core MVC开发实战之商城系统(一)
113 0
|
9月前
|
SQL 开发框架 前端开发
[回馈]ASP.NET Core MVC开发实战之商城系统(开篇)
[回馈]ASP.NET Core MVC开发实战之商城系统(开篇)
144 0
|
9月前
|
开发框架 缓存 JSON
ASP.NET Core MVC 从入门到精通之Filter
ASP.NET Core MVC 从入门到精通之Filter
120 0
|
5月前
|
开发框架 自然语言处理 前端开发
基于ASP.NET MVC开发的、开源的个人博客系统
基于ASP.NET MVC开发的、开源的个人博客系统
51 0