Difference between revisions of "DesignPatternStep2"

From mi-linux
Jump to navigationJump to search
 
(9 intermediate revisions by the same user not shown)
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
 +
 +
== Before you start ==
 +
* In order to improve readability the <? ?> markers have been omitted in all code extracts. Don't forget to add them at the beginning and end of each file!
 +
* Remember that all files should be readable by all, and all folders should be readable and executable by all, or else you will get "failed to open stream" error messages.
  
 
== Step 2 - Create the index.php file, front controller and registry class ==
 
== Step 2 - Create the index.php file, front controller and registry class ==
Line 8: Line 12:
  
 
<pre>
 
<pre>
<?
 
 
// index.php
 
// index.php
  
Line 14: Line 17:
 
require("Controller.php");
 
require("Controller.php");
 
Controller::run();
 
Controller::run();
?>
 
 
</pre>
 
</pre>
  
Line 24: Line 26:
  
 
<pre>
 
<pre>
<?
 
 
// Controller.php
 
// Controller.php
  
Line 64: Line 65:
 
   }
 
   }
 
}
 
}
?>
 
 
</pre>
 
</pre>
  
Line 70: Line 70:
  
 
<pre>
 
<pre>
<?
 
 
// Registry.php
 
// Registry.php
  
Line 101: Line 100:
 
    
 
    
 
}
 
}
?>
 
 
</pre>
 
</pre>
  

Latest revision as of 13:50, 22 September 2011

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

Before you start

  • In order to improve readability the <? ?> markers have been omitted in all code extracts. Don't forget to add them at the beginning and end of each file!
  • Remember that all files should be readable by all, and all folders should be readable and executable by all, or else you will get "failed to open stream" error messages.

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.

>> Onto Step 3