検索
ホームページ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 搭載アプリ

AI Clothes Remover

AI Clothes Remover

写真から衣服を削除するオンライン AI ツール。

Undress AI Tool

Undress AI Tool

脱衣画像を無料で

Clothoff.io

Clothoff.io

AI衣類リムーバー

AI Hentai Generator

AI Hentai Generator

AIヘンタイを無料で生成します。

ホットツール

AtomエディタMac版ダウンロード

AtomエディタMac版ダウンロード

最も人気のあるオープンソースエディター

MantisBT

MantisBT

Mantis は、製品の欠陥追跡を支援するために設計された、導入が簡単な Web ベースの欠陥追跡ツールです。 PHP、MySQL、Web サーバーが必要です。デモおよびホスティング サービスをチェックしてください。

ZendStudio 13.5.1 Mac

ZendStudio 13.5.1 Mac

強力な PHP 統合開発環境

EditPlus 中国語クラック版

EditPlus 中国語クラック版

サイズが小さく、構文の強調表示、コード プロンプト機能はサポートされていません

SecLists

SecLists

SecLists は、セキュリティ テスターの究極の相棒です。これは、セキュリティ評価中に頻繁に使用されるさまざまな種類のリストを 1 か所にまとめたものです。 SecLists は、セキュリティ テスターが必要とする可能性のあるすべてのリストを便利に提供することで、セキュリティ テストをより効率的かつ生産的にするのに役立ちます。リストの種類には、ユーザー名、パスワード、URL、ファジング ペイロード、機密データ パターン、Web シェルなどが含まれます。テスターはこのリポジトリを新しいテスト マシンにプルするだけで、必要なあらゆる種類のリストにアクセスできるようになります。