搜尋
首頁php教程php手册贯彻PHP中的MVC之控制器篇

贯彻PHP中的MVC之控制器篇

Jun 13, 2016 am 10:33 AM
mvcphp作用接受控制器簡單

The Controller 【控制器】

简单来讲,控制器的作用就是接受请求。它使用获取的方法,在这里是通过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()默认事件。
以下是具体代码部分:

  /**
  * index.php
  *
  * @author Joe Stump 
  * @copyright Joe Stump 
  * @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 
  * @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) {
          &nb

陳述
本文內容由網友自願投稿,版權歸原作者所有。本站不承擔相應的法律責任。如發現涉嫌抄襲或侵權的內容,請聯絡admin@php.cn

熱AI工具

Undresser.AI Undress

Undresser.AI Undress

人工智慧驅動的應用程序,用於創建逼真的裸體照片

AI Clothes Remover

AI Clothes Remover

用於從照片中去除衣服的線上人工智慧工具。

Undress AI Tool

Undress AI Tool

免費脫衣圖片

Clothoff.io

Clothoff.io

AI脫衣器

AI Hentai Generator

AI Hentai Generator

免費產生 AI 無盡。

熱門文章

R.E.P.O.能量晶體解釋及其做什麼(黃色晶體)
3 週前By尊渡假赌尊渡假赌尊渡假赌
R.E.P.O.最佳圖形設置
3 週前By尊渡假赌尊渡假赌尊渡假赌
R.E.P.O.如果您聽不到任何人,如何修復音頻
3 週前By尊渡假赌尊渡假赌尊渡假赌
WWE 2K25:如何解鎖Myrise中的所有內容
3 週前By尊渡假赌尊渡假赌尊渡假赌

熱工具

VSCode Windows 64位元 下載

VSCode Windows 64位元 下載

微軟推出的免費、功能強大的一款IDE編輯器

SublimeText3 Mac版

SublimeText3 Mac版

神級程式碼編輯軟體(SublimeText3)

SecLists

SecLists

SecLists是最終安全測試人員的伙伴。它是一個包含各種類型清單的集合,這些清單在安全評估過程中經常使用,而且都在一個地方。 SecLists透過方便地提供安全測試人員可能需要的所有列表,幫助提高安全測試的效率和生產力。清單類型包括使用者名稱、密碼、URL、模糊測試有效載荷、敏感資料模式、Web shell等等。測試人員只需將此儲存庫拉到新的測試機上,他就可以存取所需的每種類型的清單。

SublimeText3 英文版

SublimeText3 英文版

推薦:為Win版本,支援程式碼提示!

Dreamweaver CS6

Dreamweaver CS6

視覺化網頁開發工具