简单来讲,控制器的作用就是接受请求。它使用获取的方法,在这里是通过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"); } ?> |
| 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 |
Java Asp PHP .Net XML C/C++ CGI VB Jsp J2ee J2se J2me EJB Servlet Tomcat Resin Struts Weblogic Eclipse ANT GUI JMS Web servise IDEA Webphere Hibernate Spring Jboss Applet Swing Socket Javamail Perl Ajax P2P 安全 模式 框架 测试 开源 游戏
Windows XP Windows 2000 Windows 2003 Windows Me Windows 9.x Linux UNIX 注册表 操作系统 服务器 应用服务器