Simplifying Zend_View_Helper_Url

When I first began working with Zend_View on a project at work, I noticed that the Url view helper was a bit of a pain to use. It was rare that I didn’t want to specify one or more of the action, controller, and module in my call along with the other Route assembly parameters. Having to label them with an array index made the calls to the helper ugly and more difficult to read. As well, I often wanted to refer to actions in the same controller or controllers in the same module with respect to the current action.

To get around this, I wrote a view helper that extended Zend_View_Helper_Url and had three optional parameters specifically for the action, controller, and module. Everything else was passed into the fourth parameter, an associative array of any other parameters I needed to include. If any component of the route is excluded or specified as null, I simply get its value from the current request.

My friend Andy Best took this a step further and added in support for specifying custom routes, which he’s using for a project of his own but I didn’t need myself because I only use the default routes provided by the standard rewrite router. The result is the code below.

<?php

require_once 'Zend/Controller/Front.php';
require_once 'Zend/View/Helper/Url.php';

class App_View_Helper_Route extends Zend_View_Helper_Url
{
    public function route($action = null, $controller = null, 
        $module = null, $params = array(), $route = null)
    {
        $front = Zend_Controller_Front::getInstance();
        $request = $front->getRequest();
        $router = $front->getRouter();
        
        if ($action === null) {
            $action = $request->getActionName();
        }
        if ($controller === null) {
            $controller = $request->getControllerName();
        }
        if ($module === null) {
            $module = $request->getModuleName();
        }
        
        $urlOptions = array_merge($params, array(
            'action' => $action,
            'controller' => $controller,
            'module' => $module
        ));
        
        if ($router->hasRoute($controller) && $route === null) {
            $route = $controller;
        } else if ($route === null) {
            $route = 'default';
        }
        
        return $this->url($urlOptions, $route, true);
    }
}

5 Comments

  1. …ownership-local-se-linux-policy/ – bookmarked by 6 members
    originally found by mhl100 on 2008-09-16 Simplifying Zend_View_Helper_Url http://ishouldbecoding.com/2008/07/12/simplifying-zend_view_helper_url – bookmarked by 1 members
    or…

  2. …title=”View all posts in Development” rel=”category tag”>Development Matthew Turland has posted a quick tutorial where he aims to simplify some of the functionality that the Zend Framework’s Zend_View_Helpe…

  3. While this idea looks pretty good for just routing to controllers/actions, I’ve found at least in my projects that a lot of the time it just doesn’t work like that.

    For example, I very rarely create urls directly to a specific controller/action at least on the public side of a site. It’s because I use routes quite freely to create nicer urls, so I’m usually either passing a record ID or such as a param and just the route name.

    Personally, I would make a “flexible” url helper: It would work like the standard one does, but you could pass a string as the first param and it would read that as the route, and perhaps pass a boolean as the second, and it would read that as the reset parameter. Haven’t yet implemented this idea, but perhaps I will sometime.

  4. If you use routes that extensively, I’d say create a custom helper that takes the route name, parameters, and does what you need it to do. It doesn’t have to extend Url or include related parameters; I merely did it that way because it was convenient in my case. In yours and Andy’s cases, something more specific to custom routes makes sense.


  5. }
    That’s it. The same applies to Zend_Controller_Action_Helper_Url (there’s an example here). Posted by Federico Filed in