php控制器四

php中文网
发布: 2016-06-23 14:30:59
原创
1227人浏览过

简单来讲,控制器的作用就是接受请求。它使用获取的方法,在这里是通过uri,载入一个功能模块来刷新或者提交一个表述层。控制器将使用$_get自动全局变量来判断载入哪一个模块。

  一个请求的例子,看起来像这样:

  http://example.com/index.php?module=login

  这看起来很简单,但是在实现的过程中却不是。这里是几个控制器能识别的argument部分:

  module定义了使用哪一个模块,如users模块
  class定义了使用哪一个功能类,如你想让用户login还是logout
  event定义了使用哪一个具体事件

  这样一个更复杂的例子可以解释上面的各个argument最终组成的请求url:

  http://example.com/index.php?module=users&class=login

  这段请求告诉控制器应该载入users模块,然后是login类,最后因为没有定义具体事件,所以运行login::__default()默认事件。

  以下是具体代码部分:

<?php
 /**
  * index.php
  *
  * @author joe stump <joe@joestump.net>
  * @copyright joe stump <joe@joestump.net>
  * @license http://www.opensource.org/licenses/gpl-license.php
  * @package framework
 */

 require_once('config.php');

 // {{{ __autoload($class)
 /**
  * __autoload
  *
  * autoload is ran by php when it can't find a class it is trying to load.
  * by naming our classes intelligently we should be able to load most classes
  * dynamically.
  *
  * @author joe stump <joe@joestump.net>
  * @param string $class class name we're trying to load
  * @return void
  * @package framework
 */

 function __autoload($class)
 {
  $file = str_replace('_','/',substr($class,2)).'.php';
  require_once(fr_base_path.'/includes/'.$file);
 }
 // }}}

 if (isset($_get['module'])) {
  $module = $_get['module'];
  if (isset($_get['event'])) {
   $event = $_get['event'];
  } else {
   $event = '__default';
  }

 if (isset($_get['class'])) {
  $class = $_get['class'];
 } else {
  $class = $module;
 }

 $classfile = fr_base_path.'/modules/'.$module.'/'.$class.'.php';
 if (file_exists($classfile)) {
  require_once($classfile);
  if (class_exists($class)) {
   try {
    $instance = new $class();
    if (!fr_module::isvalid($instance)) {
     die("requested module is not a valid framework module!");
    }

    $instance->modulename = $module;
    if ($instance->authenticate()) {
     try {
      $result = $instance->$event();
      if (!pear::iserror($result)) {
       $presenter = fr_presenter::factory($instance->presenter,$instance);

      if (!pear::iserror($presenter)) {
       $presenter->display();
      } else {
       die($presenter->getmessage());
      }
     }
    } catch (exception $error) {
     die($error->getmessage());
    }
   } else {
    die("you do not have access to the requested page!");
   }
  } catch (exception $error) {
   die($error->getmessage());
  }
 } else {
  die("an valid module for your request was not found");
 }
 } else {
  die("could not find: $classfile");
 }
 } else {
  die("a valid module was not specified");
}

?>

  接下来是以上代码具体的注释:

  载入“config.php”

  定义__autoload()函数。这是php5里面的一个新函数,方便动态地载入各个类。

  如果一个argument被定义,那么载入相关的模块、类和具体事件

  接下来就是一些判断以及错误的具体操作

  最后一切无误后就载入表述层。


Pretty URLs 【友好URL】

如果你觉得上面例子讲到的请求URL让你觉得不舒服的话,那么就用mod_rewrite来实现友好URL吧。接下来是作者给这个框架写的实际重写标准代码:
RewriteEngine On
# Change the URI here to whatever you want your homepage to be
RewriteRule ^/$ /index.php?module=welcome [L,QSA]
# Changes /index.php?module=welcome to /welcome
RewriteCond %{DOCUMENT_ROOT}/%{REQUEST_FILENAME} !-d
RewriteCond %{DOCUMENT_ROOT}/%{REQUEST_FILENAME} !-f
RewriteRule ^/([^/]*)$ /index.php?module=$1 [L,QSA]
# Changes /index.php?module=users&class=login to /users/login
RewriteCond %{DOCUMENT_ROOT}/%{REQUEST_FILENAME} !-d
RewriteCond %{DOCUMENT_ROOT}/%{REQUEST_FILENAME} !-f
RewriteRule ^/([^/]*)/([^/]*)$ /index.php?module=$1&class=$2 [L,QSA]
# Changes /index.php?module=users&class=login&event=foo
# to /users/login/foo.html
RewriteCond %{DOCUMENT_ROOT}/%{REQUEST_FILENAME} !-d
RewriteCond %{DOCUMENT_ROOT}/%{REQUEST_FILENAME} !-f

RewriteRule ^/([^/]*)/([^/]*)/([^/]*).html$ \
/index.php?module=$1&class=$2&event=$3 [L,QSA]

Extending the Controller 【扩展控制器】

拥有一个集中控制器的一点好处就是你加入一些功能后,马上就能通过控制器体现出来。以下是几个可以扩展一下这个控制器的点子,使这个框架的整体能力更加强大:
你可以使用PHP5里一个新东西SoapServer来自动检测一个请求是否为SOAP

你可以使用控制器来过滤所有的自动全局变量如$_GET和$_POST以防止恶意HTML代码等

你可以使用控制器即时地转换表述层,比如从默认的方式转到PDF方式

你可以直接在控制器中加入缓存机制,这样的好处是应用程序整体都能使用到缓存以提高效率

当然,需要注意一点的是,你在控制器中所增加的功能将体现在程序全局。如你想过滤所有的自动全局变量,但是很多应用程序的管理员需要使用到一些HTML代码,反而成为一件棘手的事情(译者注:本人的想法是可以加一个if条件语句,在加载特定模块时不应用过滤功能即可)。

PHP速学教程(入门到精通)
PHP速学教程(入门到精通)

PHP怎么学习?PHP怎么入门?PHP在哪学?PHP怎么学才快?不用担心,这里为大家提供了PHP速学教程(入门到精通),有需要的小伙伴保存下载就能学习啦!

下载
来源:php中文网
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn
最新问题
开源免费商场系统广告
热门教程
更多>
最新下载
更多>
网站特效
网站源码
网站素材
前端模板
关于我们 免责申明 举报中心 意见反馈 讲师合作 广告合作 最新更新 English
php中文网:公益在线php培训,帮助PHP学习者快速成长!
关注服务号 技术交流群
PHP中文网订阅号
每天精选资源文章推送
PHP中文网APP
随时随地碎片化学习

Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号