6CC001 Workshop - week 04

From mi-linux
Revision as of 15:22, 27 July 2009 by In9352 (talk | contribs)
Jump to navigationJump to search

Main Page >> Web Application Development >> Workbook >> Week 04

Create and populate database

CREATE TABLE IF NOT EXISTS `messages` (
  `id` int(11) NOT NULL auto_increment,
  `title` varchar(255) NOT NULL,
  `message` text NOT NULL,
  `date_added` datetime NOT NULL,
  PRIMARY KEY  (`id`)
) ENGINE=MyISAM  DEFAULT CHARSET=utf8 AUTO_INCREMENT=19 ;

INSERT INTO `messages` (`id`, `title`, `message`, `date_added`) VALUES
(1, 'Test 1', 'Hello world!', '2009-07-27 15:06:29'),
(2, 'Test 2', 'Hiya', '2009-07-27 15:06:38');

Create index.php file

// The index simply calls the front controller
require("Controller.php");
Controller::run();

Create our Front Controller

require_once("Registry.php");

//############################################################################
// Front Controller class
//############################################################################
class Controller
{
  private function __construct(){}
  
  //############################################################################
  // Main "run" function
  //############################################################################    
  static function run()
  {
    $instance = new Controller;
    $instance->init();
    $instance->handleRequest();
  }
  
  //############################################################################
  // Init config stuff, e.g. database location, etc.
  //############################################################################   
  function init()
  {
    Registry::set("database_dsn", "mysql:host=localhost;dbname=YOURDBNAME");
    Registry::set("database_login", "YOURUSER");
    Registry::set("database_password", "YOURPASSWORD");
  }
  
  //############################################################################
  // Handles request from user
  //############################################################################   
  function handleRequest()
  {
    print("Hello world!");
  }
}

Create Registry class

//############################################################################
// Registry class, used to store values to be used across whole application
//############################################################################
class Registry
{
  private static $values = array();
  
  //############################################################################
  // Store a value
  //############################################################################    
  static function set($key, $val)
  {
    self::$values[$key] = $val;
  }
  
  //############################################################################
  // Get a value
  //############################################################################    
  static function get($key)
  {
    if(isset(self::$values[$key]))
    {
      return self::$values[$key];
    }
    return null;
  }
  
}

Checkpoint 1

Your page should display "Hello world!"

Create Request class

//############################################################################
// Request class
//############################################################################
class Request
{
  private $properties;

  //############################################################################
  // constructor
  //############################################################################ 
  function __construct()
  {
    $this->init();
  }
  
  //############################################################################
  // Gets all parameters from GET, POST etc...
  //############################################################################   
  function init()
  {
    $this->properties = $_REQUEST;
    return;
  }
  
  //############################################################################
  // Functions to "set" and "get" properties
  //############################################################################   
  function getProperty($key)
  {
    if(isset($this->properties[$key]))
    {
      return $this->properties[$key];
    }
  }
  function setProperty($key, $val)
  {
    $this->properties[$key] = $val;
  }
}

Create CommandResolver class

require_once("Command.php");
require_once("Command_BlogIndex.php");

//############################################################################
// CommandResolver class
//############################################################################
class CommandResolver
{
  private static $base_cmd;
  private static $default_cmd;
  
  //############################################################################
  // Constructor
  //############################################################################    
  function __construct()
  {
    if(!self::$base_cmd)
    {
      // To check if requested command is a valid command
      self::$base_cmd = new ReflectionClass("Command");
      
      // Default command to be returned if an error occurs
      self::$default_cmd = new Command_BlogIndex();
    }
  }
  
  //############################################################################
  // getCommand creates the appropriate command class, 
  // depending on parameter passed in URL
  //############################################################################    
  function getCommand(Request $request)
  {
    // Get "cmd" parameter from request
    $cmd = $request->getProperty('cmd');

    // If no command passed in, use default one    
    if(!$cmd)
      return self::$default_cmd;
      
    $filepath = "Command_{$cmd}.php";
    $classname = "Command_{$cmd}";
    
    // Check if file containing command class exists
    if(file_exists($filepath))
    {
      require_once($filepath);
      
      // Check if command class exists
      if(class_exists($classname))
      {
        $cmd_class = new ReflectionClass($classname);
        
        // Check of class is a valid command
        if($cmd_class->isSubClassOf(self::$base_cmd))
        {
          // Return new instance of command
          return $cmd_class->newInstance();
        }
      }
    }
    
    // Couldn't find command, return default command
    return clone self::$default_cmd;
  }
}

Update handleRequest() in Controller class

  //############################################################################
  // Handles request from user
  //############################################################################   
  function handleRequest()
  {
    // Create object that handles request from user
    $request = new Request();
    
    // Create object that decides which command to create depending on request
    $cmd_r = new CommandResolver();
    
    // This function return the appropriate command
    $cmd = $cmd_r->getCommand($request);
    
    print_r($cmd);
  }

Checkpoint 2