SitePoint Sponsor

User Tag List

Results 1 to 14 of 14
  1. #1
    monitormensch oerdec's Avatar
    Join Date
    Sep 2004
    Location
    Hamburg
    Posts
    706
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    [MVC] Best place to send contents?

    Hi all,

    in a MVC system, where is the best place to send the content? In my personal framework it's done in the dispatcher. I'm wondering if this is a good way to do it. I give you an example of what I mean.

    PHP Code:
    // Define a route
    $route = new Route(
      
    'blog/article/show/:title/.page'
      array(
        
    'module'     => 'Blog',
        
    'controller' => 'Article',
        
    'action'     => 'showArticle',
        
    'page'       => 1
      
    )
    );

    // Add the defined route
    $router = new Router;
    $router->addRoute($route);

    // The incoming request
    $request = new Request($_SERVER['REQUEST_URI']);

    // Dispatch!
    $dispatcher = new Dispatcher($router$request);
    $dispatcher->dispatch();


    // The Dispatcher class
    class Dispatcher {
      public function 
    dispatch() {
        
    // include the controller file and get an instance of the controller class
        
    require_once($controller_file);
        
    $controller = new $this->controller;
        
        
    // the response
        
    require_once 'Response.php';
        
    $response = new Response;
        
        
    $content $controller->{$this->action}($params);
        
    $headers $controller->getHeaders();
        
        
    // Response::send() basically sends the headers and echoes the content 
        
    $response->send($content$headers);
      }

    I hope this isn't to confusing and you know what I try to say.

    Thanks,

    oerdec

  2. #2
    SitePoint Addict Mastodont's Avatar
    Join Date
    Mar 2007
    Location
    Czech Republic
    Posts
    375
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    It looks good, but Rex Stout would probably say "too many classes"

    Do you really need Route, Router and Request? What about

    PHP Code:
    $dispatcher = new Dispatcher($_SERVER['REQUEST_URI']); 
    Route is defined as static value, so you can have it inside Dispatcher.

  3. #3
    SitePoint Wizard silver trophy kyberfabrikken's Avatar
    Join Date
    Jun 2004
    Location
    Copenhagen, Denmark
    Posts
    6,157
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I wonder about this:
    PHP Code:
      $response = new Response;
      
    $content $controller->{$this->action}($params);
      
    $headers $controller->getHeaders(); 
    I would probably prefer that the controller returned a response:
    PHP Code:
      $response $controller->{$this->action}($params);
      
    $response->send(); 

  4. #4
    SitePoint Addict Mastodont's Avatar
    Join Date
    Mar 2007
    Location
    Czech Republic
    Posts
    375
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Response is also superfluous, controller can flush all.

  5. #5
    SitePoint Wizard silver trophy kyberfabrikken's Avatar
    Join Date
    Jun 2004
    Location
    Copenhagen, Denmark
    Posts
    6,157
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by Mastodont View Post
    Response is also superfluous, controller can flush all.
    I don't think I understand what you mean by that?

  6. #6
    SitePoint Addict chestertondevelopment's Avatar
    Join Date
    Dec 2005
    Location
    Essex, UK
    Posts
    241
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by kyberfabrikken View Post
    I wonder about this:
    PHP Code:
      $response = new Response;
      
    $content $controller->{$this->action}($params);
      
    $headers $controller->getHeaders(); 
    I would probably prefer that the controller returned a response:
    PHP Code:
      $response $controller->{$this->action}($params);
      
    $response->send(); 
    I personally create the response object, pass it as a parameter to the action and then call $response->send(). I also pass the request object to the action which has methods to access the parameters. e.g.
    PHP Code:
    $response = new Response;

    // execute the action, $request defined earlier
    $controller->{$this->action}($request$response);

    // send the resposnse
    $response->send(); 

  7. #7
    SitePoint Addict Mastodont's Avatar
    Join Date
    Mar 2007
    Location
    Czech Republic
    Posts
    375
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Arrow

    Quote Originally Posted by kyberfabrikken View Post
    I don't think I understand what you mean by that?
    Something like this:

    PHP Code:
    $c = new Controller;
    if 
    $c->AppNotReady {
        
    $c->AppErrorPage();
    } else {
        if (
    $_POST) {
            
    $c->ProcessPostAndRedirect();
        } else {
            
    $c->Dispatch(new Dispatcher($_SERVER['REQUEST_URI']));
            
    $c->ProcessActions();
            
    $c->FlushPage();
        }


  8. #8
    SitePoint Addict chestertondevelopment's Avatar
    Join Date
    Dec 2005
    Location
    Essex, UK
    Posts
    241
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Response objects can include methods to create cookies, add headers and render the content, I definitely think it should be a separate object, as should Request, which can contain methods to get $_POST, $_COOKIE data etc. It's separation of responsibilities basically.

  9. #9
    SitePoint Addict Mastodont's Avatar
    Join Date
    Mar 2007
    Location
    Czech Republic
    Posts
    375
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    It depends on how deep hierarchy you want and how do you build page. In simple cases you can have view helpers hidden inside controller.

    controller is the code that gathers dynamic data and generates the content within the HTML
    http://en.wikipedia.org/wiki/Model-view-controller

    (It was only sample, I am used to separate these objects, too.)

  10. #10
    SitePoint Zealot
    Join Date
    May 2008
    Location
    Montreal
    Posts
    155
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I disagree with passing $_SERVER['REQUEST_URI'] directly to the dispatcher. The dispatchers job should be only to send a message somewhere, not validate that the message follows a certain format. REQUEST_URI is also not entirely dependable and should be checked (as with any incoming data from the client). A router class--even if it only has static methods--can be helpful for figuring out what the message ought be so that the dispatcher can focus on messaging. Also, making a router class opens up the possibility for remapping urls (in the way some frameworks do) in future without having to mangle the dispatcher.

  11. #11
    SitePoint Zealot
    Join Date
    May 2008
    Location
    Montreal
    Posts
    155
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Looking over the code again, I don't see a use for the Request object (as was pointed out). Why does it need the request uri and why not the router?

  12. #12
    monitormensch oerdec's Avatar
    Join Date
    Sep 2004
    Location
    Hamburg
    Posts
    706
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Thank you all for your comments. I've reviewed my code. Some parts make no sense for me anymore.

    At the moment the Router class has got two methods: addRoute() and getRoutes(). It is basically just a container for Route objects and hence it does not route anything. The routing is done in the dispatcher actually. I`m going to move the responsible code parts to the Router.

    @Mastodont: I don't like the idea of reducing the number of classes. Yes, everything could be done within the dispatcher. But I don't like the idea of having one big class for the entire procedure. I'd be too massive and responsible for too many different things.

    At the moment the Request class just returns the URL string. So it is unnecessary right now. But I want to add more methods for POST data, cookies etc. like stardustwd pointed out.

    I also have to rethink the Response object. Maybe it is better, when it is returned by the controller as kyberfabrikken said.

  13. #13
    SitePoint Addict Mastodont's Avatar
    Join Date
    Mar 2007
    Location
    Czech Republic
    Posts
    375
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    oerdec:
    I am advocate of less classes, not one class, but there are many ways you can go.

    Btw, I miss in your pattern some pre-dispatch phase, when no routing is needed (site is offline, blocked remote IP etc.) - simple redirect to error page suffices in these situations.

  14. #14
    monitormensch oerdec's Avatar
    Join Date
    Sep 2004
    Location
    Hamburg
    Posts
    706
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Pre-dispatching ... good point. I have to think about that.


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •