Magento中不同的*get helper*方法有什么区别?

koj*_*iro 5 magento

我已经看到了几种不同的方法来获得特定的helper,我希望有人可以解释每种方法的优缺点.例如,template/checkout/cart/sidebar/default.phtml你会看到两者$this->helper('checkout')Mage::helper('checkout').这两种方法在同一模板中有充分的理由吗?

以下是我在Magento找到帮手的所有不同方式:

abstract class Mage_Core_Block_Abstract extends Varien_Object
{
…
    /**
     * Return block helper
     *
     * @param string $type
     * @return Mage_Core_Block_Abstract
     */
    public function getHelper($type)
    {
        return $this->getLayout()->getBlockSingleton($type);
    }

    /**
     * Returns helper object
     *
     * @param string $name
     * @return Mage_Core_Block_Abstract
     */
    public function helper($name)
    {
        if ($this->getLayout()) {
            return $this->getLayout()->helper($name);
        }
        return Mage::helper($name);
    }
…
}

class Mage_Core_Model_Layout extends Varien_Simplexml_Config
{
…
    /**
     * Enter description here...
     *
     * @param string $type
     * @return Mage_Core_Helper_Abstract
     */
    public function getBlockSingleton($type)
    {
        if (!isset($this->_helpers[$type])) {
            $className = Mage::getConfig()->getBlockClassName($type);
            if (!$className) {
                Mage::throwException(Mage::helper('core')->__('Invalid block type: %s', $type));
            }

            $helper = new $className();
            if ($helper) {
                if ($helper instanceof Mage_Core_Block_Abstract) {
                    $helper->setLayout($this);
                }
                $this->_helpers[$type] = $helper;
            }
        }
        return $this->_helpers[$type];
    }

    /**
     * Retrieve helper object
     *
     * @param   string $name
     * @return  Mage_Core_Helper_Abstract
     */
    public function helper($name)
    {
        $helper = Mage::helper($name);
        if (!$helper) {
            return false;
        }
        return $helper->setLayout($this);
    }
…
}
Run Code Online (Sandbox Code Playgroud)

Vin*_*nai 10

Mage_Core_Block_Abstract::getHelper()

Mage_Core_Model_Layout::getBlockSingleton()方法不返回Magento辅助对象,而是返回Magento对象类型块的实例.
我相信这是遗留代码,例如该Mage::getBlockSingleton()方法已被弃用.

在这两种情况下,都是从Magento类id创建一个块实例.

该方法getBlockSingleton()将实例存储在$_helpers布局对象的属性中,该方法createBlock()将其存储在$_blocks属性中.

只能$_blocks使用布局XML引用(和覆盖)数组中的块.

getBlockSingleton()如果您想要特定块类的实例,并且您希望确保不创建块的新实例(如果它已经存在),则该方法很有用.
要通过createBlock()您创建的实例(几乎)实现相同的效果,需要以下代码:

public function alternativeGetBlockSingleton($classId)
{
    foreach (Mage::app()->getLayout()->getAllBlocks() as $block)
    {
        if ($block->getType() == $classId)
        {
            return $block;
        }
    }
    return $this->createBlock($classId);
}
Run Code Online (Sandbox Code Playgroud)

Mage_Core_Block_Abstract::helper()

Mage_Core_Block_Abstract::helper()方法返回Magento中通常被称为帮助程序的实例.直接
调用的唯一区别Mage::helper($name)是布局对象被设置为辅助实例上的属性.

有人可能会争辩说,$this->helper()在模板文件中使用比较干净Mage::helper(),因为这会减少硬编码引用的数量(从而减少对Mage类的依赖),但是在Magento的情况下,这个参数是徒劳的,因为每个模块都非常依赖于Mage某些模块Mage_Core无论如何这些课程.

在实践中,可能没有功能上的理由偏爱另一个,除了这Mage::helper()是更常见和众所周知的,其他开发人员阅读代码会更少混淆,这使得它更易于维护.

另一方面,Magento是关于选择的,并且有很多方法可以完成给定的任务.