PHP Objects, Patterns and Practice (3rd edition)

(Barry) #1
CHAPTER 12 ■ ENTERPRISE PATTERNS

Here is a simple View Helper class:

namespace woo\view;
class VH {
static function getRequest() {
return \woo\base\RequestRegistry::getRequest();
}
}


All this class does at present is provide access to a Request object. You can extend it to provide
additional functionality as your application evolves. If you find yourself doing something in a view that
takes up more than a couple of lines, chances are it belongs in the View Helper. In a larger application,
you may provide multiple View Helper objects in an inheritance hierarchy in order to provide different
tools for different parts of your system.
Here is a simple view that uses both the View Helper and the Request object:


<?php
require_once("woo/view/ViewHelper.php");
$request = \woo\view\VH::getRequest(); // Controller caches this
$venue = $request->getObject('venue'); // Command caches this
?>




Add a Space for venue <?php echo $venue->getName() ?>


Add a Space for Venue 'getName() ?>'







getFeedbackString("
"); ?>


value="getProperty( 'space_name' ) ?>" name="space_name"/>





The view (add_space.php) gets a Request object from the View Helper (VH) and uses its methods to
supply the dynamic data for the page. In particular, the getFeedback() method returns any messages set
by commands, and getObject() acquires any objects cached for the view layer. getProperty() is used to
access any parameters set in the HTTP request. If you run this view on its own, the Venue and Request

Free download pdf