View Helper sets base URL.>
The HTML in listing 6.4 is very simple as we just need the user to supply two fields to us: username and
password. As you can see in (#1), we use a view helper, LinkTo(), to create the correct URL to the
auth/identify controller action. This View Helper is shown in listing 6.5 and exists to encapsulate the retrieval
of the base URL from the request. The view script itself is not interested in such details which is why a view
helper is the ideal solution.
Listing 6.5 LinkTo() View Helper
class Zend_View_Helper_LinkTo
{
protected static $baseUrl = null; #1
public function linkTo($path)
{
if (self::$baseUrl === null) {
$request = Zend_Controller_Front::getInstance()->getRequest();
$root = '/' . trim($request->getBaseUrl(), '/');
if ($root == '/') {
$root = '';
}
self::$baseUrl = $root . '/';
}
Licensed to Menshu You
Please post comments or corrections to the Author Online forum at
http://www.manning-sandbox.com/forum.jspa?forumID=329
return self::$baseUrl . ltrim($path, '/');
}
}
(annotation) <#1. Cache the baseURL.>
The LinkTo() function uses a static variable (#1) to cache the creation of the base URL. This minor
optimization is to save the time taken to call through the multiple functions to get at the value from the front
controller??™s request property.
Pages:
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192