|
 |
栏目导栏 |
|
| |
|
|
|
|
 |
资料搜索 |
|
| |
|
|
|
|
 |
热门文章 |
|
| |
|
|
|
|
 |
最新文章 |
|
| |
|
|
|
| |
| |
|
|
|
|
简单来讲,控制器的作用就是接受请求。它使用获取的方法,在这里是通过URI,载入一个功能模块来刷新或者提交一个表述层。控制器将使用$_GET自动全局变量来判断载入哪一个模块。 75wLinux联盟 75wLinux联盟 一个请求的例子,看起来像这样: 75wLinux联盟 75wLinux联盟 http://example.com/index.PHP?module=login 75wLinux联盟 75wLinux联盟 这看起来很简单,但是在实现的过程中却不是。这里是几个控制器能识别的argument部分: 75wLinux联盟 75wLinux联盟 module定义了使用哪一个模块,如users模块 75wLinux联盟 class定义了使用哪一个功能类,如你想让用户login还是logout 75wLinux联盟 event定义了使用哪一个具体事件 75wLinux联盟 75wLinux联盟 这样一个更复杂的例子可以解释上面的各个argument最终组成的请求URL: 75wLinux联盟 75wLinux联盟 http://example.com/index.php?module=users&class=login 75wLinux联盟 75wLinux联盟 这段请求告诉控制器应该载入users模块,然后是login类,最后因为没有定义具体事件,所以运行login::__default()默认事件。 75wLinux联盟 75wLinux联盟 以下是具体代码部分: 75wLinux联盟 75wLinux联盟 <?php 75wLinux联盟 /** 75wLinux联盟 * index.php 75wLinux联盟 * 75wLinux联盟 * @author Joe Stump <joe@joestump.net> 75wLinux联盟 * @copyright Joe Stump <joe@joestump.net> 75wLinux联盟 * @license http://www.opensource.org/licenses/gpl-license.php 75wLinux联盟 * @package Framework 75wLinux联盟 */ 75wLinux联盟 75wLinux联盟 require_once('config.php'); 75wLinux联盟 75wLinux联盟 // {{{ __autoload($class) 75wLinux联盟 /** 75wLinux联盟 * __autoload 75wLinux联盟 * 75wLinux联盟 * Autoload is ran by PHP when it can't find a class it is trying to load. 75wLinux联盟 * By naming our classes intelligently we should be able to load most classes 75wLinux联盟 * dynamically. 75wLinux联盟 * 75wLinux联盟 * @author Joe Stump <joe@joestump.net> 75wLinux联盟 * @param string $class Class name we're trying to load 75wLinux联盟 * @return void 75wLinux联盟 * @package Framework 75wLinux联盟 */ 75wLinux联盟 75wLinux联盟 function __autoload($class) 75wLinux联盟 { 75wLinux联盟 $file = str_replace('_','/',substr($class,2)).'.php'; 75wLinux联盟 require_once(FR_BASE_PATH.'/includes/'.$file); 75wLinux联盟 } 75wLinux联盟 // }}} 75wLinux联盟 75wLinux联盟 if (isset($_GET['module'])) { 75wLinux联盟 $module = $_GET['module']; 75wLinux联盟 if (isset($_GET['event'])) { 75wLinux联盟 $event = $_GET['event']; 75wLinux联盟 } else { 75wLinux联盟 $event = '__default'; 75wLinux联盟 } 75wLinux联盟 75wLinux联盟 if (isset($_GET['class'])) { 75wLinux联盟 $class = $_GET['class']; 75wLinux联盟 } else { 75wLinux联盟 $class = $module; 75wLinux联盟 } 75wLinux联盟 75wLinux联盟 $classFile = FR_BASE_PATH.'/modules/'.$module.'/'.$class.'.php'; 75wLinux联盟 if (file_exists($classFile)) { 75wLinux联盟 require_once($classFile); 75wLinux联盟 if (class_exists($class)) { 75wLinux联盟 try { 75wLinux联盟 $instance = new $class(); 75wLinux联盟 if (!FR_Module::isValid($instance)) { 75wLinux联盟 die("Requested module is not a valid framework module!"); 75wLinux联盟 } 75wLinux联盟 75wLinux联盟 $instance->moduleName = $module; 75wLinux联盟 if ($instance->authenticate()) { 75wLinux联盟 try { 75wLinux联盟 $result = $instance->$event(); 75wLinux联盟 if (!PEAR::isError($result)) { 75wLinux联盟 $presenter = FR_Presenter::factory($instance->presenter,$instance); 75wLinux联盟 75wLinux联盟 if (!PEAR::isError($presenter)) { 75wLinux联盟 $presenter->display(); 75wLinux联盟 } else { 75wLinux联盟 die($presenter->getMessage()); 75wLinux联盟 } 75wLinux联盟 } 75wLinux联盟 } catch (Exception $error) { 75wLinux联盟 die($error->getMessage()); 75wLinux联盟 } 75wLinux联盟 } else { 75wLinux联盟 die("You do not have Access to the requested page!"); 75wLinux联盟 } 75wLinux联盟 } catch (Exception $error) { 75wLinux联盟 die($error->getMessage()); 75wLinux联盟 } 75wLinux联盟 } else { 75wLinux联盟 die("An valid module for your request was not found"); 75wLinux联盟 } 75wLinux联盟 } else { 75wLinux联盟 die("Could not find: $classFile"); 75wLinux联盟 } 75wLinux联盟 } else { 75wLinux联盟 die("A valid module was not specified"); 75wLinux联盟 } 75wLinux联盟 75wLinux联盟 ?> 75wLinux联盟 75wLinux联盟 接下来是以上代码具体的注释: 75wLinux联盟 75wLinux联盟 载入“config.php” 75wLinux联盟 75wLinux联盟 定义__autoload()函数。这是PHP5里面的一个新函数,方便动态地载入各个类。 75wLinux联盟 75wLinux联盟 如果一个argument被定义,那么载入相关的模块、类和具体事件 75wLinux联盟 75wLinux联盟 接下来就是一些判断以及错误的具体操作 75wLinux联盟 75wLinux联盟 最后一切无误后就载入表述层 75wLinux联盟 75wLinux联盟 【友好URL】 75wLinux联盟 75wLinux联盟 如果你觉得上面例子讲到的请求URL让你觉得不舒服的话,那么就用mod_rewrite来实现友好URL吧。接下来是作者给这个框架写的实际重写标准代码: 75wLinux联盟 75wLinux联盟 RewriteEngine On 75wLinux联盟 75wLinux联盟 # Change the URI here to whatever you want your homepage to be 75wLinux联盟 75wLinux联盟 RewriteRule ^/$ /index.php?module=welcome [L,QSA] 75wLinux联盟 75wLinux联盟 # Changes /index.php?module=welcome to /welcome 75wLinux联盟 75wLinux联盟 RewriteCond %{DOCUMENT_ROOT}/%{REQUEST_FILENAME} !-d 75wLinux联盟 75wLinux联盟 RewriteCond %{DOCUMENT_ROOT}/%{REQUEST_FILENAME} !-f 75wLinux联盟 75wLinux联盟 RewriteRule ^/([^/]*)$ /index.php?module=$1 [L,QSA] 75wLinux联盟 75wLinux联盟 # Changes /index.php?module=users&class=login to /users/login 75wLinux联盟 75wLinux联盟 RewriteCond %{DOCUMENT_ROOT}/%{REQUEST_FILENAME} !-d 75wLinux联盟 75wLinux联盟 RewriteCond %{DOCUMENT_ROOT}/%{REQUEST_FILENAME} !-f 75wLinux联盟 75wLinux联盟 RewriteRule ^/([^/]*)/([^/]*)$ /index.php?module=$1&class=$2 [L,QSA] 75wLinux联盟 75wLinux联盟 # Changes /index.php?module=users&class=login&event=foo 75wLinux联盟 75wLinux联盟 # to /users/login/foo.html 75wLinux联盟 75wLinux联盟 RewriteCond %{DOCUMENT_ROOT}/%{REQUEST_FILENAME} !-d 75wLinux联盟 75wLinux联盟 RewriteCond %{DOCUMENT_ROOT}/%{REQUEST_FILENAME} !-f 75wLinux联盟 75wLinux联盟 RewriteRule ^/([^/]*)/([^/]*)/([^/]*).html$ \ 75wLinux联盟 75wLinux联盟 /index.php?module=$1&class=$2&event=$3 [L,QSA] 75wLinux联盟 75wLinux联盟 Extending the Controller 75wLinux联盟 75wLinux联盟 【扩展控制器】 75wLinux联盟 75wLinux联盟 拥有一个集中控制器的一点好处就是你加入一些功能后,马上就能通过控制器体现出来。以下是几个可以扩展一下这个控制器的点子,使这个框架的整体能力更加强大: 75wLinux联盟 75wLinux联盟 你可以使用PHP5里一个新东西SoapServer来自动检测一个请求是否为SOAP 75wLinux联盟 75wLinux联盟 你可以使用控制器来过滤所有的自动全局变量如$_GET和$_POST以防止恶意HTML代码等 75wLinux联盟 75wLinux联盟 你可以使用控制器即时地转换表述层,比如从默认的方式转到PDF方式 75wLinux联盟 75wLinux联盟 你可以直接在控制器中加入缓存机制,这样的好处是应用程序整体都能使用到缓存以提高效率 75wLinux联盟 75wLinux联盟 当然,需要注意一点的是,你在控制器中所增加的功能将体现在程序全局。如你想过滤所有的自动全局变量,但是很多应用程序的管理员需要使用到一些HTML代码,反而成为一件棘手的事情(译者注:本人的想法是可以加一个if条件语句,在加载特定模块时不应用过滤功能即可)。 75wLinux联盟
Linux联盟收集整理 ,转贴请标明原始链接,如有任何疑问欢迎来本站Linux论坛讨论 |
|
|
|
|
|