Magento의 모든 .phtml에서 지정된 클래스를 확장하고 새로 정의 된 함수를 어디에서나 호출 할 수있는 Magento 2 모듈을 만들려면 어떻게해야합니까?phtml의 어디서나 액세스 모듈 기능
나는이 블록 내 모듈을 만드는 시도했다 :
등 /을 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 (내가 필요한 곳)
하지만 Magento에서는 허용하지 않습니다.
위의 함수는 아무 것도 울리지 않으므로 함수는 NULL입니다.
"Magento는 내가 그것을 할 수 없다"는 것을 의미합니까? 직면 한 문제에 대해 구체적으로 설명하십시오. – tvo
@tvo 함수는 NULL을 반환합니다. –