Difference between revisions of "DesignPatternStep2"

From mi-linux
Jump to navigationJump to search
Line 1: Line 1:
 
[[Main Page]] >> [[6CC001|Advanced Web Technologies]] >> [[6CC001 - Workbook|Workbook]] >> [[6CC001 Workshop - week 04|Week 04]] >> Step 2 - Create the index.php file, front controller and registry class
 
[[Main Page]] >> [[6CC001|Advanced Web Technologies]] >> [[6CC001 - Workbook|Workbook]] >> [[6CC001 Workshop - week 04|Week 04]] >> Step 2 - Create the index.php file, front controller and registry class
 +
 +
== Step 2 - Create the index.php file, front controller and registry class ==
 +
 +
As explained in the lecture, our application has a single entry point that handles all user requests: index.php
 +
 +
Well, index.php doesn’t really do much. It simply creates and runs the Front Controller object:
 +
 +
<pre>
 +
// index.php
 +
 +
// The index simply calls the front controller
 +
require("Controller.php");
 +
Controller::run();
 +
</pre>
 +
 +
Next, let's have a look at the Controller class itself.
 +
 +
Index.php calls the run function, which creates an instance of the Front Controller, and then runs a couple of internal functions:
 +
* init() does some initial set-up. In our case, it saves the database details to the registry.
 +
* handleRequest() does the actual work. Let’s keep it simple for now.
 +
 +
<pre>
 +
// Controller.php
 +
 +
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!");
 +
  }
 +
}
 +
</pre>
 +
 +
The Registry class is a simple front-end for an array. You can pass it values using the set() function, and retrieve them later using the get() function.
 +
 +
<pre>
 +
// Registry.php
 +
 +
//############################################################################
 +
// 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;
 +
  }
 +
 
 +
}
 +
</pre>
 +
 +
===Checkpoint===
 +
 +
It is still early days!
 +
 +
If you browse to the location of your index.php file, you should see "Hello world!". It doesn’t seem much, but we are already creating our Front Controller, and getting it to save our database connection details to the Registry.
 +
 +
We now need to handle the actual user request, i.e. display the page that the user asked for.

Revision as of 16:42, 12 July 2011

Main Page >> Advanced Web Technologies >> Workbook >> Week 04 >> Step 2 - Create the index.php file, front controller and registry class

Step 2 - Create the index.php file, front controller and registry class

As explained in the lecture, our application has a single entry point that handles all user requests: index.php

Well, index.php doesn’t really do much. It simply creates and runs the Front Controller object:

// index.php

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

Next, let's have a look at the Controller class itself.

Index.php calls the run function, which creates an instance of the Front Controller, and then runs a couple of internal functions:

  • init() does some initial set-up. In our case, it saves the database details to the registry.
  • handleRequest() does the actual work. Let’s keep it simple for now.
// Controller.php

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!");
  }
}

The Registry class is a simple front-end for an array. You can pass it values using the set() function, and retrieve them later using the get() function.

// Registry.php

//############################################################################
// 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

It is still early days!

If you browse to the location of your index.php file, you should see "Hello world!". It doesn’t seem much, but we are already creating our Front Controller, and getting it to save our database connection details to the Registry.

We now need to handle the actual user request, i.e. display the page that the user asked for.