Front Controller try, what is bad?

Coding Critique is the place to post source code for peer review by other members of DevNetwork. Any kind of code can be posted. Code posted does not have to be limited to PHP. All members are invited to contribute constructive criticism with the goal of improving the code. Posted code should include some background information about it and what areas you specifically would like help with.

Popular code excerpts may be moved to "Code Snippets" by the moderators.

Moderator: General Moderators

Post Reply
tua1
Forum Commoner
Posts: 28
Joined: Thu May 15, 2008 9:30 pm

Front Controller try, what is bad?

Post 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
User avatar
Christopher
Site Administrator
Posts: 13596
Joined: Wed Aug 25, 2004 7:54 pm
Location: New York, NY, US

Re: Front Controller try, what is bad?

Post by Christopher »

Looks ok. Does it work? I would not use static/Singletons and remove the View code.
(#10850)
tua1
Forum Commoner
Posts: 28
Joined: Thu May 15, 2008 9:30 pm

Re: Front Controller try, what is bad?

Post 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?
User avatar
Christopher
Site Administrator
Posts: 13596
Joined: Wed Aug 25, 2004 7:54 pm
Location: New York, NY, US

Re: Front Controller try, what is bad?

Post 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.
(#10850)
tua1
Forum Commoner
Posts: 28
Joined: Thu May 15, 2008 9:30 pm

Re: Front Controller try, what is bad?

Post 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
User avatar
Christopher
Site Administrator
Posts: 13596
Joined: Wed Aug 25, 2004 7:54 pm
Location: New York, NY, US

Re: Front Controller try, what is bad?

Post 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?
(#10850)
tua1
Forum Commoner
Posts: 28
Joined: Thu May 15, 2008 9:30 pm

Re: Front Controller try, what is bad?

Post by tua1 »

Hmmy it's difficult questions for me now. I'll remove view code from controller.
Thanks again for your advices.
Post Reply