Page 1 of 1

Front Controller try, what is bad?

Posted: Mon Jan 04, 2010 1:52 pm
by tua1
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.

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();
 
Regards

Re: Front Controller try, what is bad?

Posted: Mon Jan 04, 2010 8:18 pm
by Christopher
Looks ok. Does it work? I would not use static/Singletons and remove the View code.

Re: Front Controller try, what is bad?

Posted: Tue Jan 05, 2010 5:35 am
by tua1
Thanks for your response.

Could you tell the about remove singleton?
And waht about view, move code to the other class, maybe ApplicationController?

Re: Front Controller try, what is bad?

Posted: Tue Jan 05, 2010 12:11 pm
by Christopher
tua1 wrote:Could you tell the about remove singleton?
In general, I would recommend using instantiating objects until you are forced to static calls.
tua1 wrote:And waht about view, move code to the other class, maybe ApplicationController?
I would move the view code to a View class.

Re: Front Controller try, what is bad?

Posted: Tue Jan 05, 2010 5:18 pm
by tua1
Maybe something like ViewController?

Responsibilities:

- find correct view class by name from config file
- send the data to the loaded view class (do some processing before eg. explode array etc).
- call view class display function

Re: Front Controller try, what is bad?

Posted: Tue Jan 05, 2010 6:54 pm
by Christopher
tua1 wrote:Maybe something like ViewController?
The is a Controller and there is a View. You can combine those responsibilities, but there is no such thing as a ViewController...
tua1 wrote:Responsibilities:

- find correct view class by name from config file
- send the data to the loaded view class (do some processing before eg. explode array etc).
- call view class display function
You are going to have every View-Controller relationship specified in the config? Typically some convention is used so it is easy to load a View with the same name as the Controller/Action. But what about complex View that may have multiple sub-Views? What about layouts?

Re: Front Controller try, what is bad?

Posted: Wed Jan 06, 2010 5:51 am
by tua1
Hmmy it's difficult questions for me now. I'll remove view code from controller.
Thanks again for your advices.