0

最初の Magento 拡張機能を作成しようとしていますが、すでに最初のステップで立ち往生しています。

CMSのページやブロックに追加{{block type="rick_printer/print" text="Hello world"}}する際に「Hello world」を表示させたい。残念ながら何も起こりません。これが私のコードです:

app\code\local\Rick\Printer\etc\config.xml

<?xml version="1.0" encoding="UTF-8"?>
<config> 
    <modules>
        <Rick_Printer>
            <version>0.0.1</version>
        </Rick_Printer>
    </modules>
    <global>
        <blocks>
            <rick_printer>
                <class>Rick_Printer_Block_Print</class>
            </rick_printer>
        </blocks>
    </global>
</config>

app\etc\modules\Rick_Printer.xml

<?xml version="1.0" encoding="UTF-8"?>
<config>
    <modules>
        <Rick_Printer>
            <active>true</active>
            <codePool>local</codePool>
        </Rick_Printer>
    </modules>
</config>

app\code\local\Rick\Printer\Block\Print.php

class Rick_Printer_Block_Print extends Mage_Core_Block_Abstract
{  
    protected function _construct()
    {
        $this->setTemplate('rick/printer/view.phtml');
        parent::_construct();
    }

    public function printIt()
    {
        $text = $this->getText();
        if (!$text) {
            $msg = "Please provide a text!";
            echo $msg;
            return array();
        }        
        return $text;
    }
}

app\design\frontend\default\default\template\rick\printer\print.phtml

<?php
    $text = $this->spinIt();
    echo $text;
?>

私はコードが醜いことを知っており、おそらくすべて間違っています。どんな助けでも大歓迎です!

ありがとう


更新: Vinai の回答から修正を適用した後、私のapp\code\local\Rick\Printer\etc\config.xmlは次のようになります。

<?xml version="1.0" encoding="UTF-8"?>
<config> 
    <modules>
        <Rick_Printer>
            <version>0.0.1</version>
        </Rick_Printer>
    </modules>
    <global>
        <blocks>
            <rick_printer>
                <class>Rick_Printer_Block</class>
            </rick_printer>
        </blocks>
    </global>
</config>

CMS ページにアクセスすると、次のエラー メッセージが表示されます (これは表示されません)。

class Rick_Printer_Block_Print extends Mage_Core_Block_Abstract
{  
protected function _construct()
    {
        $this->setTemplate('rick/printer/view.phtml');  
        parent::_construct();
    }  

    public function printIt()
    {
        $text = $this->getText();  
        if (!$text) {
            $msg = "Please provide a text!";  
            echo $msg; return array();
        }
        return $text;
    }
}


Fatal error: Class 'Rick_Printer_Block_Print' not found in /home/www/xyz/htdocs/app/code/core/Mage/Core/Model/Layout.php on line 491
4

1 に答える 1

2

まず、ブロック クラスのプレフィックスが間違っています。ノード名は<class>ですが、実際に指定しているのはクラス プレフィックスです。
別の見方をすると、このノードは、モジュールのブロックが配置されているディレクトリを宣言しています。
正しい方法は

<!-- language: xml -->
<global>
    <blocks>
        <rick_printer>
            <class>Rick_Printer_Block</class>
        </rick_printer>
    </blocks>
</global>

次に、テンプレートで$this->spinIt();の代わりに呼び出しています$this->printIt();。ただのタイプミス...

それ以外の場合、コードは問題ないように見えます。

更新 エラー メッセージは、クラス名へのファイル システム パスが一致していないことを示します。大文字と小文字を確認し、タイプミスがないか確認してください。
また、ブロックでテンプレートをレンダリングする必要があるため、ブロック クラスの代わりに拡張Rick_Printer_Block_Printする必要があります。Mage_Core_Block_Template_Abstract

于 2013-10-17T08:42:27.737 に答える