Front Controller & Command Execution
Posted: Wed Aug 09, 2006 4:29 pm
I just whipped this up... please rip it to shreds. Let me know Every little nit-picky thing that is wrong with it.
EDIT: oh yea... almost forgot to say what it is... if it's not blatently obvious, it's a front controller and command resolver
I'm hoping that maybe with the community's help, I can perfect this and replace my current (more or less procedural) page controllers
EDIT: oh yea... almost forgot to say what it is... if it's not blatently obvious, it's a front controller and command resolver
I'm hoping that maybe with the community's help, I can perfect this and replace my current (more or less procedural) page controllers
Code: Select all
<?php
require_once 'C:\wamp\www\core\classes\Request.inc.php';
// COMMANDS
class Front_Command{
protected $action;
public function execute(){
echo "Error: Could not find specified command";
}
public function doExecute( Front_Command $command ){
$command->execute();
}
}
class Front_Command_default extends Front_Command{
public function __construct(){
$this->action = "Default";
}
public function execute(){
echo $this->action . " was executed.";
}
}
class Front_Command_login extends Front_Command{
public function __construct(){
$this->action = "Login";
}
public function execute(){
echo $this->action . " was executed.";
}
}
// ENDCOMMANDS
// Request is just a class that gets request info (from url, forms, command line, etc.)
// Front request is specialized in that it returns information specific to the front controller
class Front_Request extends Request{
protected $default;
protected $error;
public function __construct($default, $error){
parent::__construct();
$this->default = $default;
$this->error = $error;
}
public function getCommand(){
return $this->action ? $this->action : $this->default;
}
public function getCommandDefault(){
return $this->default;
}
public function getCommandError(){
return $this->error;
}
}
class Front_Command_Resolver{
public function getCommandComponent( Front_Request $request ){
// Get command from request object
$command = $request->getCommand();
// Assign class name with command appended
$class_name = "Front_Command_{$command}";
// If this specific class exists, return it
if(class_exists($class_name)){
return new $class_name();
}
// Otherwise return the standard command
return new Front_Command();
}
}
class Front_Controller{
private static $instance = null;
private function __construct(){
// Some sort of initialization
}
public function getInstance(){
// This shouldn't need an explanation, but I'll explain anyway... it ensures that only one instance of this can be instantiated
if( ! self::$instance ) {
self::$instance = new Front_Controller();
}
return self::$instance;
}
public function run(){
// Get request from user (could be by url/command line... not up to this class to decide... left up to the front request object)
$request = new Front_Request('default', 'error');
// Get an instance of the command resolver
$command_resolver = new Front_Command_Resolver();
// Resolve the user's command into a command component (a descendant of our friend ^^ the Front_Command
$command = $command_resolver->getCommandComponent( $request );
// Execute the command
$command->execute();
}
}
$Controller = Front_Controller::getInstance();
$Controller->run();
?>