Difference between revisions of "6CC001 Workshop - week 04"
From mi-linux
Jump to navigationJump to searchLine 23: | Line 23: | ||
require("Controller.php"); | require("Controller.php"); | ||
Controller::run(); | Controller::run(); | ||
+ | </pre> | ||
+ | |||
+ | == Create our Front Controller == | ||
+ | |||
+ | <pre> | ||
+ | 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=alix"); | ||
+ | Registry::set("database_login", "root"); | ||
+ | Registry::set("database_password", "R3dstr1pe"); | ||
+ | } | ||
+ | |||
+ | //############################################################################ | ||
+ | // Handles request from user | ||
+ | //############################################################################ | ||
+ | function handleRequest() | ||
+ | { | ||
+ | print("Hello world!"); | ||
+ | } | ||
+ | } | ||
</pre> | </pre> | ||
Revision as of 15:14, 27 July 2009
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 (17, 'Test 1', 'Hello world!', '2009-07-27 15:06:29'), (18, '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=alix"); Registry::set("database_login", "root"); Registry::set("database_password", "R3dstr1pe"); } //############################################################################ // 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; } }