Difference between revisions of "DesignPatternStep2"

From mi-linux
Jump to navigationJump to search
Line 8: Line 8:
  
 
<pre>
 
<pre>
<?
 
 
// index.php
 
// index.php
  
Line 14: Line 13:
 
require("Controller.php");
 
require("Controller.php");
 
Controller::run();
 
Controller::run();
?>
 
 
</pre>
 
</pre>
  
Line 24: Line 22:
  
 
<pre>
 
<pre>
<?
 
 
// Controller.php
 
// Controller.php
  
Line 64: Line 61:
 
   }
 
   }
 
}
 
}
?>
 
 
</pre>
 
</pre>
  
Line 70: Line 66:
  
 
<pre>
 
<pre>
<?
 
 
// Registry.php
 
// Registry.php
  
Line 101: Line 96:
 
    
 
    
 
}
 
}
?>
 
 
</pre>
 
</pre>
  

Revision as of 13:23, 22 September 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.

>> Onto Step 3