1

カスタム モジュール内に 1 つのブロックを作成しましたが、機能しません。私のモジュールは正常に動作していますが、私のブロックを footer.phtml ファイルから呼び出すと、ブロックが機能せず、「致命的なエラー: オブジェクト以外のメンバー関数 setTemplate() への呼び出し」が表示されます。実際、カスタムブロックを使用してフロントエンドにメッセージを表示したいのですが、以下のコードについて言及しています

local/Monojit/Custom/controllers/IndexController.php

<?php
class Monojit_Custom_IndexController extends Mage_Core_Controller_Front_Action
{
   public function indexAction()
    {   

        $this->loadLayout();    

        $block = $this->getLayout()->createBlock(
            'Mage_Core_Block_Template',
            'my_block_name_here',
            array('template' => 'custom/test.phtml')
        );

        $this->getLayout()->getBlock('content')->append($block);
        $this->renderLayout();
    }

}
?>

local/Monojit/Custom/Block/Mycustomblock.php

<?php
class Monojit_Custom_Block_Mycustomblock extends Mage_Core_Block_Template

{
//public function _prepareLayout()
//    {
//      return parent::_prepareLayout();
//    }

    public function _construct() {     
        parent::_construct(); 
        $this->setTemplate('custom/test.phtml');     
    }
    public function getmessage()
     {
       $msg = "showing my custom block";       
       return $msg;
     }   
} 
?>

ローカル/モノジット/カスタム/etc/config.xml

<config>

<global>

<modules>

<monojit_custom>

<version>0.1.0</version>

</monojit_custom>

</modules>

<blocks>

<custom>

<rewrite>

<custom>Monojit_Custom_Block_Mycustomblock</custom>

</rewrite>

</custom>

</blocks>
<helpers>
  <custom>
      <class>Monojit_Custom_Helper</class>
  </custom>
</helpers>
</global>

<frontend>

<routers>

<custom>

<use>standard</use>

<args>

<module>Monojit_Custom</module>

<frontName>custom</frontName>

</args>

</custom>

</routers>

<layout>

<updates>

<custom>

<file>custom.xml</file>

</custom>

</updates>

</layout>

</frontend>

</config>

フロントエンド/デフォルト/モノジット内に1つのテーマ(コピーされたモダンテーマ)を作成しました管理者のデザイン構成も変更し、skin.screenshot pic内にも必要なフォルダーを作成しましたここに画像の説明を入力

デザイン/フロントエンド/デフォルト/モノジット/テンプレート/カスタム/test.phtml

//want to fetch data from my block 
<p>This is your custom block called programatically.</p>

localhost/magento/index.php/custom ページにはメッセージが正しく表示されますが、footer.phtml ページからブロックを呼び出すと

<?php echo $this->getLayout()->createBlock('custom/mycustomblock')->setTemplate('custom/test.phtml')->toHtml(); ?>

'Fatal error: Call to a member function setTemplate() on an object'' と表示されます。layout.xml ファイルを作成する必要がありますか? 問題を解決する方法を教えてください。ありがとうございます

4

2 に答える 2