理解PHP中的MVC編程之控制器_PHP教程

來源:互聯網
上載者:User
  簡單來講,控制器的作用就是接受請求。它使用擷取的方法,在這裡是通過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 cant 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 were 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();

http://www.bkjia.com/PHPjc/508703.htmlwww.bkjia.comtruehttp://www.bkjia.com/PHPjc/508703.htmlTechArticle簡單來講,控制器的作用就是接受請求。它使用擷取的方法,在這裡是通過URI,載入一個功能模組來重新整理或者提交一個表述層。控制器將使...

  • 聯繫我們

    該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

    如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

    A Free Trial That Lets You Build Big!

    Start building with 50+ products and up to 12 months usage for Elastic Compute Service

    • Sales Support

      1 on 1 presale consultation

    • After-Sales Support

      24/7 Technical Support 6 Free Tickets per Quarter Faster Response

    • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.