当前位置:中国站长下载文章中心网页编程PHP编程 → 理解PHP中的MVC编程之MVC框架简介(3)

理解PHP中的MVC编程之MVC框架简介(3)

减小字体 增大字体 作者:不详  来源:不详  发布时间:2006-8-14 10:14:48
     auth.php-这是所有验证功能的底层类。它是从FR_Module里面延伸出来的,主要功能是定义一个基本的验证类如何工作。
  
    跟FR_Module的道理一样,有些类不需要链接到数据库,那么同理,FR_Auth_No就可以被创建应用到不需要验证功能的类上。
  
  <?php
   abstract class FR_Auth extends FR_Module
   {
    // {{{ __construct()
    function __construct()
    {
     parent::__construct();
    }
    // }}}
    // {{{ authenticate()
     abstract function authenticate();
    // }}}
  
    // {{{ __destruct()
  
     function __destruct()
     {
      parent::__destruct();
     }
    // }}}
   }
  
  ?>
  
  module.php-所有模块的心脏
  
  <?php
   abstract class FR_Module extends FR_Object_Web
   {
    // {{{ properties
    /**
    * $presenter
    *
    * Used in FR_Presenter::factory() to determine which presentation (view)
    * class should be used for the module.
    *
    * @author Joe Stump <joe@joestump.net>
    * @var string $presenter
    * @see FR_Presenter, FR_Presenter_common, FR_Presenter_smarty
    */
    public $presenter = 'smarty';
    /**
    * $data
    *
    * Data set by the module that will eventually be passed to the view.
    *
    * @author Joe Stump <joe@joestump.net>
    * @var mixed $data Module data
    * @see FR_Module::set(), FR_Module::getData()
    */
  
    protected $data = array();
  
    /**
    * $name
    *
    * @author Joe Stump <joe@joestump.net>
    * @var string $name Name of module class
    */
  
    public $name;
  
    /**
    * $tplFile
    *
    * @author Joe Stump <joe@joestump.net>
    * @var string $tplFile Name of template file
    * @see FR_Presenter_smarty
    */
  
    public $tplFile;
  
    /**
    * $moduleName
    *
    * @author Joe Stump <joe@joestump.net>
    * @var string $moduleName Name of requested module
    * @see FR_Presenter_smarty
    */
  
    public $moduleName = null;
    /**
    * $pageTemplateFile
    *
    * @author Joe Stump <joe@joestump.net>
    * @var string $pageTemplateFile Name of outer page template
    */
  
    public $pageTemplateFile = null;
    // }}}
  
    // {{{ __construct()
    /**
    * __construct
    *
    * @author Joe Stump <joe@joestump.net>
    */
  
    public function __construct()
    {
     parent::__construct();
     $this->name = $this->me->getName();
     $this->tplFile = $this->name.'.tpl';
    }
  
    // }}}
    // {{{ __default()
  
    /**
    * __default
    *
    * This function is ran by the controller if an event is not specified
    * in the user's request.
    *
    * @author Joe Stump <joe@joestump.net>
    */
  
    abstract public function __default();
    // }}}
    // {{{ set($var,$val)
  
    /**
    * set
    *
    * Set data for your module. This will eventually be passed toe the
    * presenter class via FR_Module::getData().
    *
    * @author Joe Stump <joe@joestump.net>
    * @param string $var Name of variable
    * @param mixed $val Value of variable
    * @return void
    * @see FR_Module::getData()
    */
  
    protected function set($var,$val) {
     $this->data[$var] = $val;
    }
    // }}}
    // {{{ getData()
  
    /**
    * getData
    *
    * Returns module's data.
    *
    * @author Joe Stump <joe@joestump.net>
    * @return mixed
    * @see FR_Presenter_common
    */
  
    public function getData()
    {
     return $this->data;
    }
    // }}}
    // {{{ isValid($module)
  
    /**
    * isValid
    *
    * Determines if $module is a valid framework module. This is used by
    * the controller to determine if the module fits into our framework's
    * mold. If it extends from both FR_Module and FR_Auth then it should be
    * good to run.
    *
    * @author Joe Stump <joe@joestump.net>
    * @static
    * @param mixed $module
    * @return bool
    */
  
    public static function isValid($module)
    {
     return (is_object($module) &&
     $module instanceof FR_Module &&
     $module instanceof FR_Auth);
    }
    // }}}
    // {{{ __destruct()
  
    public function __destruct()
    {
     parent::__destruct();
    }
    // }}}
   }
  ?>
  
  presenter.php-表述层的核心。
  
  <?php
   class FR_Presenter
   {
    // {{{ factory($type,FR_Module $module)
    /**
    * factory  
    *
    * @author Joe Stump <joe@joestump.net>
    * @access public
    * @param string $type Presentation type (our view)
    * @param mixed $module Our module, which the presenter will display
    * @return mixed PEAR_Error on failure or a valid presenter
    * @static
    */
  
    static public function factory($type,FR_Module $module)
    {
     $file = FR_BASE_PATH.'/includes/Presenter/'.$type.'.php';
     if (include($file)) {
      $class = 'FR_Presenter_'.$type;
      if (class_exists($class)) {
       $presenter = new $class($module);
       if ($presenter instanceof FR_Presenter_common) {
        return $presenter;
       }
       return PEAR::raiseError('Invalid presentation class: '.$type);
      }
      return PEAR::raiseError('Presentation class not found: '.$type);
     }
     return PEAR::raiseError('Presenter file not found: '.$type);
    }
    // }}}
   }
  
  ?>
  
    下一篇里,我将介绍控制器(MVC中的Controller,本文的index.php)的构造。第三篇里,我将介绍表述层(MVC里面的View)。第四篇里,我将用具体模块为例建立一个应用(MVC里面的Module或Model)。
    做人要厚道,请注明转自chinazhan中国站长(www.ChinaZhan.com)。