Search code examples
zend-frameworkpartial-viewszend-view

Where is the implementation for the ->partial() method in Zend Framework?


I'm trying to find where Zend implemented the partial() method so that I can add some ACL code into it for permission control. I know that the calling object of the method is always a Zend_View object, and I looked, but the partial method seems to be missing in it (and all the classes/interfaces that it inherits/implements).

Does anybody know how Zend_View acquires that method?

For those of you who are optimization minded and are itching to tell me that I should use render() instead, the partials in question require variable inputs as they play the same kind of roles as a table row partial does. Unless you can show me how to do that with a render() method, please stay on topic.

Also, I'm not intending to modify the Zend Framework code directly, I simply want to override the implementation, but I also want to see the method I'm overriding so that I can see what I'm working with.


Solution

  • How to write your own helpers and override default helpers

    Since we're talking about a view helper, the code for it can be found in library\Zend\View\Helper\Partial.php. If you want to change what the partial view helper does, write your own helper:

    class MyProject_View_Helper_MyHelper extends Zend_View_Helper_Abstract {}
    

    or

    class MyProject_View_Helper_MyHelper implements Zend_View_Helper_Interface {}
    

    and then add this helper to the helper stack by adding the path to it to your configuration.

    resources.view.helperPath.MyProject_View_Helper_ = "MyProject/View/Helper/"
    

    If you call your helper Partial, you can completely override the default partial helper.

    See also this blog about view helpers by Akrabat.

    And, to put it in your words...

    ...how Zend_View aquires the partial method

    If a helper is registered, it can be called from the view because of the following code in Zend_View_Abstract

    /**
     * Accesses a helper object from within a script.
     *
     * If the helper class has a 'view' property, sets it with the current view
     * object.
     *
     * @param string $name The helper name.
     * @param array $args The parameters for the helper.
     * @return string The result of the helper output.
     */
    public function __call($name, $args)
    {
        // is the helper already loaded?
        $helper = $this->getHelper($name);
    
        // call the helper method
        return call_user_func_array(
            array($helper, $name),
            $args
        );
    }