Understanding MVC in PHP
Pages: 1, 2, 3, 4, 5, 6

Auth.php

This is the base class for authentication. It extends from the FR_Module class from Module.php. Its major function is to define how a basic authentication class should behave.

An alternate approach is to define this as a variable in the module and then have the controller create the authentication module through a factory pattern. However, this way works too (and is simpler to explain).

Child classes should override the authenticate() method. The controller will use this method when determining if a user has access to the given module. For instance, the FR_Auth_No class simply returns true, which allows you to create modules that require no authentication.


<?php

  abstract class FR_Auth extends FR_Module
  {
      // {{{ __construct()
      function __construct()
      {
          parent::__construct();
      }
      // }}}
      // {{{ authenticate()
      abstract function authenticate();
      // }}}
      // {{{ __destruct()
      function __destruct()
      {
          parent::__destruct();
      }
      // }}}
  }

?>

Module.php

This is the heart of all of the modules. It extends the FR_Object_DB class and provides all of its children with database access and an open log file.

Additionally, it defines the default presentation layer, the default template file for the module, default page template file, and a few other variables that the controller and presentation layer use.

The class also provides the basic structure of what each module must possess as far as functions. The function set() abstracts the method of setting data into a centralized place, which the getData() function then hands off to the presentation layer for rendering.


<?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();
      }
      // }}}
  }

?>

Pages: 1, 2, 3, 4, 5, 6

Next Pagearrow