PHP DevCenter

oreilly.comSafari Books Online.Conferences.

We've expanded our LAMP news coverage and improved our search! Search for all things LAMP across O'Reilly!

Search
Search Tips

advertisement

Listen Print Discuss Subscribe to PHP Subscribe to Newsletters

Implementing MVC in PHP: The View
Pages: 1, 2

Other Presenters

You can create other presenters, as well. I've created one called debug.php that simply displays various debugging information. You could change your model class's $presenter to debug and it would render completely differently.



Additionally, you could create a presentation layer called rest.php that outputs the model class's $data variable as well-formed XML. If your model detected a REST request, it would switch the presenter by assigning rest to $this->presenter.

<?php

  /**
  * FR_Presenter_rest
  *
  * @author Joe Stump <joe@joestump.net>
  * @copyright Joe Stump <joe@joestump.net>
  * @license http://www.opensource.org/licenses/gpl-license.php
  * @package Framework
  * @filesource
  */

  require_once('XML/Serializer.php');

  /**
  * FR_Presenter_rest
  *
  * Want to display your module's data in valid XML rather than HTML? This
  * presenter will automatically take your data and output it in valid XML.
  *
  * @author Joe Stump <joe@joestump.net>
  * @package Framework
  */
  class FR_Presenter_rest extends FR_Presenter_common
  {
      // {{{ __construct(FR_Module $module)
      /**
      * __construct
      *
      * @author Joe Stump <joe@joestump.net>
      * @access public
      * @param mixed $module Instance of FR_Module
      * @return void
      */
      public function __construct(FR_Module $module)
      {
          parent::__construct($module);
      }
      // }}}
      // {{{ display()
      /**
      * display
      *
      * Output our data array using the PEAR package XML_Serializer. This may
      * not be the optimal output you want for your REST API, but it should
      * display valid XML that can be easily consumed by anyone.
      *
      * @author Joe Stump <joe@joestump.net>
      * @return void
      * @link http://pear.php.net/package/XML_Serializer
      */
      public function display()
      {
          $xml = new XML_Serializer();
          $xml->serialize($this->module->getData());

          header("Content-Type: text/xml");
          echo '<?xml version="1.0" encoding="UTF-8" ?>'."\n";
          echo $xml->getSerializedData();
      }
      // }}}
      // {{{ __destruct()
      public function __destruct()
      {
          parent::__destruct();
      }
      // }}}
  }

?>

In the REST presentation layer I use the PEAR package XML_Serializer to output the FR_Module::$data array as valid XML. It's not extremely intuitive, but it does allow me to output my module in valid XML. I use this REST presentation layer later on in one of my applications.

Conclusion

You get the idea. The presentation layer is an extremely flexible way of displaying the model. To make things even better you can dynamically switch the presenter in the model before the controller renders the module class via the presentation layer.

Before I move on, I'd like to plant the seed for another presentation layer. How about using htmldoc in a presenter named pdf to render your module class as a PDF document?

Up Next

The next article will covering the model portion of the MVC framework, to which my framework also refers as "modules" and "module classes" up to this point. My example model will be a simple module to log people in and out of the system. After that you'll be on your own to build up my little framework into something useful!

Joe Stump is the Lead Architect for Digg where he spends his time partitioning data, creating internal services, and ensuring the code frameworks are in working order.


Return to the PHP DevCenter.


What would you like Joe to cover next? Let him know here.
You must be logged in to the O'Reilly Network to post a talkback.
Post Comment
Full Threads Oldest First

Showing messages 1 through 1 of 1.

  • debug presenter will not work
    2007-11-21 05:24:55  borkedcoder [Reply | View]

    because Variable variables: Superglobals cannot be used as variable variables inside functions or class methods.

    http://us2.php.net/manual/en/language.variables.predefined.php#language.variables.superglobals


Tagged Articles

Be the first to post this article to del.icio.us

Sponsored Resources

  • Inside Lightroom
Advertisement

Sponsored by:

O'Reilly Media

©2009, O'Reilly Media, Inc.
(707) 827-7000 / (800) 998-9938
All trademarks and registered trademarks appearing on oreilly.com are the property of their respective owners.
About O'Reilly
Academic Solutions
Authors
Contacts
Customer Service
Jobs
Newsletters
O'Reilly Labs
Press Room
Privacy Policy
RSS Feeds
Terms of Service
User Groups
Writing for O'Reilly
Content Archive
Business Technology
Computer Technology
Google
Microsoft
Mobile
Network
Operating System
Digital Photography
Programming
Software
Web
Web Design
More O'Reilly Sites
O'Reilly Radar
Ignite
Tools of Change for Publishing
Digital Media
Inside iPhone
O'Reilly FYI
makezine.com
craftzine.com
hackszine.com
perl.com
xml.com

Partner Sites
InsideRIA
java.net
O'Reilly Insights on Forbes.com