• <ruby id="5koa6"></ruby>
    <ruby id="5koa6"><option id="5koa6"><thead id="5koa6"></thead></option></ruby>

    <progress id="5koa6"></progress>

  • <strong id="5koa6"></strong>
  • 理解PHP中的MVC編程之控制器

    發表于:2007-09-07來源:作者:點擊數: 標簽:
    簡單來講,控制器的作用就是接受請求。它使用獲取的方法,在這里是通過URI,載入一個功能模塊來刷新或者提交一個表述層??刂破鲗⑹褂?_GET自動全局變量來判斷載入哪一個模塊。 一個請求的例子,看起來像這樣: http://example.com/index.php?module=login 這
    簡單來講,控制器的作用就是接受請求。它使用獲取的方法,在這里是通過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 aclearcase/" target="_blank" >ccess 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被定義,那么載入相關的模塊、類和具體事件

      接下來就是一些判斷以及錯誤的具體操作

      最后一切無誤后就載入表述層。


    熱門推薦 化繁為簡 QQ最實用的六大絕招 QQ2005 Beta3功能搶先使用評測
    <

    原文轉自:http://www.kjueaiud.com

    老湿亚洲永久精品ww47香蕉图片_日韩欧美中文字幕北美法律_国产AV永久无码天堂影院_久久婷婷综合色丁香五月

  • <ruby id="5koa6"></ruby>
    <ruby id="5koa6"><option id="5koa6"><thead id="5koa6"></thead></option></ruby>

    <progress id="5koa6"></progress>

  • <strong id="5koa6"></strong>