<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>Evilprofessor.co.uk &#187; custom</title>
	<atom:link href="http://www.evilprofessor.co.uk/tag/custom/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.evilprofessor.co.uk</link>
	<description>The website of Steven Lloyd Watkin</description>
	<lastBuildDate>Thu, 04 Aug 2011 09:56:21 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.2.1</generator>
		<item>
		<title>Creating URL in Zend Custom View Helper</title>
		<link>http://www.evilprofessor.co.uk/239-creating-url-in-zend-custom-view-helper/</link>
		<comments>http://www.evilprofessor.co.uk/239-creating-url-in-zend-custom-view-helper/#comments</comments>
		<pubDate>Thu, 28 Jan 2010 23:01:57 +0000</pubDate>
		<dc:creator>Steven Lloyd Watkin</dc:creator>
				<category><![CDATA[Articles]]></category>
		<category><![CDATA[PHP]]></category>
		<category><![CDATA[Web Programming]]></category>
		<category><![CDATA[Zend Framework]]></category>
		<category><![CDATA[Zend_View]]></category>
		<category><![CDATA[Zend_View_Helper]]></category>
		<category><![CDATA[custom]]></category>
		<category><![CDATA[framework]]></category>
		<category><![CDATA[front controller]]></category>
		<category><![CDATA[helper]]></category>
		<category><![CDATA[Router]]></category>
		<category><![CDATA[view]]></category>
		<category><![CDATA[zend]]></category>
		<category><![CDATA[Zend_View_Helper_Abstract]]></category>

		<guid isPermaLink="false">http://www.evilprofessor.co.uk/?p=239</guid>
		<description><![CDATA[This may seem simple, but I was banging my head trying to create a URL in a custom view helper in Zend Framework. I have routing setup which gets the module from the sub-domain in use so I couldn&#8217;t use a simple hardcoded URL. Basically but invoking an instance of the front controller its possible [...]]]></description>
			<content:encoded><![CDATA[<p>This may seem simple, but I was banging my head trying to create a URL in a custom view helper in <a href="http://framework.zend.com" class="kblinker" target="_blank" title="More about Zend Framework &raquo;">Zend Framework</a>. I have routing setup which gets the module from the sub-domain in use so I couldn&#8217;t use a simple hardcoded URL.</p>
<p><span id="more-239"></span></p>
<p>Basically but invoking an instance of the front controller its possible to grab the router and assemble a url. Assemble is the function used in the view helper. The URL is built up from an array of module, controller, action, etc, followed by a second parameter of the route to use. The code is as follows:</p>
<pre name="code" class="php">&lt;?php
/**
 * View helper which returns link category URL
 *
 * @author     Lloyd Watkin
 * @since      25/01/2010
 * @package    ViewHelper
 * @subpackage LinksUrl
 */
class Pro_View_Helper_LinksUrl
    extends Zend_View_Helper_Abstract
{
	/**
	 * Returns link category URL
	 *
	 * @param  Doctrine_Record $category
	 * @param  string          $module
	 * @param  string          $controller
	 * @param  string          $action
	 * @return string Url
	 */
    public function linksUrl($category, $module = 'www',
        $controller = 'links', $action = 'index')
    {
    	$router = Zend_Controller_Front::getInstance()-&gt;getRouter();

        return $router-&gt;assemble(array(
            'module'     =&gt; $module,
            'controller' =&gt; $controller,
            'action'     =&gt; $action,
            'category'   =&gt; "{$category-&gt;id}-{$category-&gt;name}",
        ), 'www-index');
    }
}</pre>
<p>Another way to do this is to invoke Zend_View_Helper_Url itself and call the Url method (if you want to use the helper itself). This can be done by using the following code:</p>
<pre class="php" name="code">&lt;?php
/**
 * View helper which returns link category URL
 *
 * @author     Lloyd Watkin
 * @since      25/01/2010
 * @package    ViewHelper
 * @subpackage LinksUrl
 */
class Pro_View_Helper_LinksUrl
    extends Zend_View_Helper_Abstract
{
	/**
	 * Returns link category URL
	 *
	 * @param  Doctrine_Record $category
	 * @param  string          $module
	 * @param  string          $controller
	 * @param  string          $action
	 * @return string Url
	 */
    public function linksUrl($category, $module = 'www',
        $controller = 'links', $action = 'index')
    {
    	$link = new Zend_View_Helper_Url();

        return $link-&gt;url(array(
            'module'     =&gt; $module,
            'controller' =&gt; $controller,
            'action'     =&gt; $action,
            'category'   =&gt; "{$category-&gt;id}-{$slug}",
        ), 'www-index');
    }
}</pre>
<p>Both almost identical. Not a hard thing to do in the framework but can catch you out ;)</p>
<iframe src="http://www.facebook.com/plugins/like.php?href=http%3A%2F%2Fwww.evilprofessor.co.uk%2F239-creating-url-in-zend-custom-view-helper%2F&amp;layout=standard&amp;show_faces=true&amp;width=450&amp;action=like&amp;colorscheme=light&amp;height=80" scrolling="no" frameborder="0" style="border:none; overflow:hidden; width:450px; height:80px;" allowTransparency="true"></iframe>]]></content:encoded>
			<wfw:commentRss>http://www.evilprofessor.co.uk/239-creating-url-in-zend-custom-view-helper/feed/</wfw:commentRss>
		<slash:comments>3</slash:comments>
		</item>
	</channel>
</rss>
<!-- WP Super Cache is installed but broken. The path to wp-cache-phase1.php in wp-content/advanced-cache.php must be fixed! -->
