【问题标题】:Access Module functions everywhere in phtml在 phtml 中随处可见的访问模块功能
【发布时间】:2016-09-08 14:30:27
【问题描述】:

如何创建一个扩展给定类的 Magento 2 模块,并能够在 Magento 的每个 .phtml 中的任何位置调用我新定义的函数?

我已经尝试创建我的模块,这是块:

etc/module.xml

<?xml version="1.0"?>
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="../../../../../lib/internal/Magento/Framework/Module/etc/module.xsd">
    <module name="Chapagain_HelloWorld" setup_version="1.0.0" schema_version="1.0.0">
        <sequence>
            <module name="Magento_Footer"/>
        </sequence>
    </module>
</config>

块/HelloWorld.php

<?php
namespace Chapagain\HelloWorld\Block;
class HelloWorld extends \Magento\Framework\View\Element\Template
{
    protected $_storeManager;    

    public function __construct(
        \Magento\Backend\Block\Template\Context $context,        
        \Magento\Store\Model\StoreManagerInterface $storeManager,        
        array $data = []
    )
    {        
        $this->_storeManager = $storeManager;        
        parent::__construct($context, $data);
    }

    /**
     * Get store identifier
     *
     * @return  int
     */
    public function getStoreId()
    {
        return $this->_storeManager->getStore()->getId();
    }

    /**
     * Get website identifier
     *
     * @return string|int|null
     */
    public function getWebsiteId()
    {
        return $this->_storeManager->getStore()->getWebsiteId();
    }

    /**
     * Get Store code
     *
     * @return string
     */
    public function getStoreCode()
    {
        return $this->_storeManager->getStore()->getCode();
    }

    /**
     * Get Store name
     *
     * @return string
     */
    public function getStoreName()
    {
        return $this->_storeManager->getStore()->getName();
    }

    /**
     * Get current url for store
     *
     * @param bool|string $fromStore Include/Exclude from_store parameter from URL
     * @return string     
     */
    public function getStoreUrl($fromStore = true)
    {
        return $this->_storeManager->getStore()->getCurrentUrl($fromStore);
    }

    /**
     * Check if store is active
     *
     * @return boolean
     */
    public function isStoreActive()
    {
        return $this->_storeManager->getStore()->isActive();
    }
}
?>

稍后我尝试调用 footer.phtml 和 header.phtml 中的函数(我需要它们的地方)

<?php 
echo $block->getStoreId() . '<br />';
echo $block->getStoreCode() . '<br />';
echo $block->getWebsiteId() . '<br />';
echo $block->getStoreName() . '<br />';
echo $block->getStoreUrl() . '<br />';
echo $block->isStoreActive() . '<br />';
?>

但 Magento 不允许我这样做。

上面的函数没有回显,函数为 NULL。

【问题讨论】:

  • “Magento 不允许我这样做”是什么意思?具体说明您面临的问题。
  • @tvo 函数返回 NULL。

标签: magento magento2


【解决方案1】:

确保您正在扩展正确的块。如果要扩展页眉或页脚,请找到原始位置,而不是退出几个文件夹,直到找到 layouts 文件夹,浏览其中的文件,直到找到调用此模板的位置。你会看到它附着在某种块上。复制/粘贴它的名称,并将其放入您的“Class *** extends [paste here]”中。现在随意编写您的代码。现在,在您的模块中创建一个与原始布局文件同名的文件,并具有相同的路径(我的意思是布局/文件夹之后的路径)。使用 XML setTemplate 操作将块的模板替换为您的模板(可以通过添加 'Your_Module::[path to your template after /templates folder]' 来完成)。你几乎准备好了。现在打开您的模板覆盖并添加一些对您的自定义方法的调用。如果一切都正确完成,您应该会看到正确的输出。

还有一些提示:

  • 不要将“_”用于受保护的变量。只需使用“storeManager”而不是“storeManager”。
  • 更好的类型使用[您要依赖的类]。它可以帮助您的编码清洁剂。所以代替这个:Class HelloWorld extends \Magento\Framework\View\Element\Template,使用这个:use Magento\Framework\View\Element\Template; HelloWorld 类扩展模板

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2012-05-30
    • 1970-01-01
    • 1970-01-01
    • 2019-12-30
    • 1970-01-01
    • 2020-05-09
    • 1970-01-01
    相关资源
    最近更新 更多