2016-09-08 1 views
2

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입니다.

+0

"Magento는 내가 그것을 할 수 없다"는 것을 의미합니까? 직면 한 문제에 대해 구체적으로 설명하십시오. – tvo

+0

@tvo 함수는 NULL을 반환합니다. –

답변

0

오른쪽 블록을 확장하고 있는지 확인하십시오. 머리글이나 바닥 글을 확장하려면 원래 위치를 찾으십시오.이 폴더를 찾을 때까지 몇 개의 폴더를 뒤로 내밀어 레이아웃 폴더를 찾을 때까지 파일을 찾아 봅니다. 어떤 종류의 블록에 붙어있는 것을 볼 수 있습니다. 그 이름을 복사하여 붙여 넣은 다음 '*** *** [여기에 붙여 넣기] 확장'에 넣으십시오. 이제 코드를 자유롭게 코딩하십시오. 이제 모듈의 원래 레이아웃 파일과 동일한 이름의 파일을 만듭니다.이 파일은 동일한 경로 (레이아웃/폴더 이후의 경로)를가집니다. XML setTemplate 액션을 사용하여 블록의 템플릿을 바꿉니다 ('Your_Module :: [템플릿의 경로/템플릿 폴더]'를 추가하여 수행 할 수 있습니다). 거의 준비가되었습니다. 이제 템플릿 재정의를 열고 사용자 정의 메서드에 대한 호출을 추가하십시오. 모든 작업이 올바르게 수행되면 올바른 결과가 나타납니다. 또한

몇 가지 팁 :

  • 보호 변수 '_'사용하지 마십시오. 'storeManager'대신 'storeManager'를 사용하십시오.
  • 더 나은 유형의 사용 [당신이 의존하는 수업]. 코드 작성자에게 도움이됩니다. 따라서 다음과 같이하십시오 : HelloWorld 클래스는 \ Magento \ Framework \ View \ Element \ Template을 확장합니다. : Magento \ Framework \ View \ Element \ Template을 사용하십시오. 클래스 HelloWorld는 템플릿을 확장합니다