Zend Framework

Registering helpers with views

Details

  • Type: Improvement Improvement
  • Status: Resolved Resolved
  • Priority: Minor Minor
  • Resolution: Fixed
  • Affects Version/s: 1.9.5
  • Fix Version/s: 1.10.0
  • Component/s: Zend_View
  • Labels:
    None

Description

From a request on the FW-MVC mailing list, Zend_View should offer a way to register helper objects without relying on the plugin loader. This would allow for dependency injection, objects that don't follow the PEAR naming convention, and PHP 5.3 namespaces.

  1. ViewTest.diff
    29/Oct/09 11:29 AM
    0.7 kB
    Hector Virgen
  2. Zend_View_Abstract.diff
    29/Oct/09 11:29 AM
    0.8 kB
    Hector Virgen

Activity

Hide
Hector Virgen added a comment -

I'm putting together a patch that will allow registering custom helpers like this:

// Adding a custom helper
$view = new Zend_View();
$helper = new MyCustomHelper();
$view->addHelper($helper, 'foo');
$view->foo(); // calls MyCustomHelper#foo()

// Overwriting a built-in helper
$myUrlHelper = new MyUrlHelper();
$view->addHelper($myUrlHelper, 'url');
$view->url(); // calls MyUrlHelper#url();

Any thoughts on this?

Show
Hector Virgen added a comment - I'm putting together a patch that will allow registering custom helpers like this: // Adding a custom helper $view = new Zend_View(); $helper = new MyCustomHelper(); $view->addHelper($helper, 'foo'); $view->foo(); // calls MyCustomHelper#foo() // Overwriting a built-in helper $myUrlHelper = new MyUrlHelper(); $view->addHelper($myUrlHelper, 'url'); $view->url(); // calls MyUrlHelper#url(); Any thoughts on this?
Hide
Avi Block added a comment -

I'm wondering now if it's a good idea also to include anonymous functions here as well (a la Zend_Validate_Callback). Of course, one could always create a "Callback" helper this way as well, which would accomplish the same purpose, without having to change things around too much.

Show
Avi Block added a comment - I'm wondering now if it's a good idea also to include anonymous functions here as well (a la Zend_Validate_Callback). Of course, one could always create a "Callback" helper this way as well, which would accomplish the same purpose, without having to change things around too much.
Hide
Hector Virgen added a comment -

What would that code look like? So far I have this:

/**

  • Registers a helper
    *
  • @param Zend_View_Helper_Abstract|object $helper
  • @param string $name
  • @return Zend_View_Abstract
    */
    public function registerHelper($helper, $name)
    {
    if (!is_object($helper)) { throw new Zend_View_Exception('View helper must be an object.', $this); }
    $this->_helper[$name] = $helper;
    return $this;
    }
Show
Hector Virgen added a comment - What would that code look like? So far I have this: /**
  • Registers a helper *
  • @param Zend_View_Helper_Abstract|object $helper
  • @param string $name
  • @return Zend_View_Abstract */ public function registerHelper($helper, $name) { if (!is_object($helper)) { throw new Zend_View_Exception('View helper must be an object.', $this); } $this->_helper[$name] = $helper; return $this; }
Hide
Hector Virgen added a comment -

Sorry, forgot to add the code markup.. this should look better:

Zend_View_Abstract Patch (Draft)
/**
 * Registers a helper
 *
 * @param Zend_View_Helper_Abstract|object $helper
 * @param string $name
 * @return Zend_View_Abstract
 */
public function registerHelper($helper, $name)
{
    if (!is_object($helper)) {
        throw new Zend_View_Exception('View helper must be an object.', $this);
    }
    $this->_helper[$name] = $helper;
    return $this;
}
Show
Hector Virgen added a comment - Sorry, forgot to add the code markup.. this should look better:
Zend_View_Abstract Patch (Draft)
/**
 * Registers a helper
 *
 * @param Zend_View_Helper_Abstract|object $helper
 * @param string $name
 * @return Zend_View_Abstract
 */
public function registerHelper($helper, $name)
{
    if (!is_object($helper)) {
        throw new Zend_View_Exception('View helper must be an object.', $this);
    }
    $this->_helper[$name] = $helper;
    return $this;
}
Hide
Avi Block added a comment -

What you have here seems to me to be sufficient for the main purpose of this feature (of course it must be tested!). As for adding in the anonymous function capabilities, you would probably have to change the __call method of Zend_View_Abstract, because that method is expecting a class and a method.

Show
Avi Block added a comment - What you have here seems to me to be sufficient for the main purpose of this feature (of course it must be tested!). As for adding in the anonymous function capabilities, you would probably have to change the __call method of Zend_View_Abstract, because that method is expecting a class and a method.
Hide
Hector Virgen added a comment -

I don't have a CLA yet but I've attached the patch and passing unit test.

Show
Hector Virgen added a comment - I don't have a CLA yet but I've attached the patch and passing unit test.
Hide
Hector Virgen added a comment -

Please see attached files for fix and passing unit test.

Show
Hector Virgen added a comment - Please see attached files for fix and passing unit test.
Hide
Matthew Weier O'Phinney added a comment -

Re-opening. Attaching a patch does not resolve an issue; committing code to the repository does. I'll review the patch for inclusion.

Show
Matthew Weier O'Phinney added a comment - Re-opening. Attaching a patch does not resolve an issue; committing code to the repository does. I'll review the patch for inclusion.
Hide
Hector Virgen added a comment -

Thanks, Matthew. I don't have SVN commit privileges (that I know of). How can I submit a patch for review?

Show
Hector Virgen added a comment - Thanks, Matthew. I don't have SVN commit privileges (that I know of). How can I submit a patch for review?
Hide
Matthew Weier O'Phinney added a comment -

You just did.

Seriously, though, attaching patches to the issue tracker is the best way to do so. If you notice no action on one, feel free to bug the component lead (either directly, or via the zf-contributors mailing list).

Thanks!

Show
Matthew Weier O'Phinney added a comment - You just did. Seriously, though, attaching patches to the issue tracker is the best way to do so. If you notice no action on one, feel free to bug the component lead (either directly, or via the zf-contributors mailing list). Thanks!
Hide
Matthew Weier O'Phinney added a comment -

Patches applied (with additional tests and slight changes in functionality) to trunk; will release with 1.10.

Show
Matthew Weier O'Phinney added a comment - Patches applied (with additional tests and slight changes in functionality) to trunk; will release with 1.10.

People

Vote (1)
Watch (1)

Dates

  • Created:
    Updated:
    Resolved: