Front Controller try, what is bad?
Posted: Mon Jan 04, 2010 1:52 pm
Hello
I am creating a small framework for my oop and patterns skill improvement.
All my thoughts are included in the docs.
Sorry for my english but I do my best.
I hope somebody read this and give me advices.
Regards
I am creating a small framework for my oop and patterns skill improvement.
All my thoughts are included in the docs.
Sorry for my english but I do my best.
I hope somebody read this and give me advices.
Code: Select all
class FrontController {
private static $instance;
/**
* FrontController::run()
*
* Creates Request, Response objects, store them into Registry and start handling request
* @access public
*/
public static function run() {
if (empty(self::$instance))
self::$instance = new self();
$request = new Request();
$response = new Response();
$registry = Registry::getRegistry('http');
$registry->setRequest($request);
$registry->setResponse($response);
self::$instance->handleRequest($request, $response);
}
/**
* Handling http request
*
* Method create ApplicationController object, and send Request object for command resolving, if
* success, command is executed. In my implemenentation Command object acting like Controller, calling
* for model connected with the command (xml config) and if model generating any results (search result
* etc) these results are storing in Response object.
* If command has chain of command, all commands are executed, after that, front controller get
* adequate view object bounded with the model.
*
*
* @access public
* @param Request $req
* @param Response $res
*/
public function handleRequest(Request $req, Response $res) {
echo 'Handling request...';
$appCtrl = new ApplicationController();
while($command = $appCtrl->getCommand($req, $res)) {
$command->execute();
}
$view = $appCtrl->getView();
$this->invokeView($view, $res);
}
/**
* @access public
* @param View $view
* @param Response $res
*
* if Response has any cookies to send, or other things that need to send before generate view flush()
* method will do it.
* if Response has parameters (which probably always will be true, maybe this if has no sense) the data from * Response i assign to View object.
*
* The view is than display.
*
*/
public function invokeView(View $view, Response $res) {
$res->flush();
if ($res->hasParameters()) {
$view->setData($res->getParameters());
}
$view->display();
}
private function __construct() {}
}
FrontController::run();