1

Magento 1.7.0.2 を実行しています (Ubuntu Linux 12.04 LTS 上)。

Alan Storm の No Frills Magento Layout 本のセクション 1.8 に触発された Magento コードのセクションを取得しようとしています。まず、そのセクションの Mage_Core_Template_Block は Mage_Core_Block_Template と読むべきだと思います。

しかし、もっと重要なことは、$block = $layout->createBlock('foo_bar/hello') への呼び出しが false を返し、exception.log で例外をスローすることです。これは、foo の前に Mage を付けているため、代わりに存在しない Mage_Foo_Bar_Block_Hello というオブジェクトを参照しているためです。私が求めているタイプ Foo_Bar_Block_Hello のオブジェクトに戻ります。カスタマイズ ファイルの内容とトリガー URL は次のとおりです。


エラーを引き起こす URL:

http:// localhost/magpractice/helloworld/index/layout/

/var/www/magpractice/app/code/local/Alanstormdotcom/Helloworld/controllers/IndexController.php

<?php

class Alanstormdotcom_Helloworld_IndexController extends Mage_Core_Controller_Front_Action {

  function layoutAction() {

    $layout = Mage::getSingleton('core/layout');
    $block = $layout->createBlock('alanstormdotcom_helloworld/hello', 'root');
    var_dump($block);
    echo $block->toHtml();

  }

}

?>

/var/www/magpractice/app/code/local/Alanstormdotcom/Helloworld/Block/Hello.php

<?php
class Alanstormdotcom_Helloworld_Block_Hello extends Mage_Core_Block_Template {

  public function _construct() {

    $this->setTemplate('helloworld.phtml');
    return parent::_construct();

  }

  public function _beforeToHtml() {

    $block_1 = new Mage_Core_Block_Text();
    $block_1->setText('The first sentence.');
    $this->setChild('the_first', $block_1);

  }

  public function fetchTitle() {
    return 'Hello Fancy World!';
  }

}
?>

/var/www/magpractice/app/code/local/Alanstormdotcom/Helloworld/etc/config.xml

<config>
  <modules>
    <Alanstormdotcom_Helloworld>
      <version>0.1.0</version>
    </Alanstormdotcom_Helloworld>
  </modules>
  <frontend>
    <routers>
      <helloworld>
        <use>standard</use>
        <args>
          <module>Alanstormdotcom_Helloworld</module>
          <frontName>helloworld</frontName>
        </args>
      </helloworld>
    </routers>
  </frontend>
</config>

/var/www/magpractice/var/log の内容は次のとおりです。

2013-06-11T12:42:56+00:00 ERR (3):
exception 'Mage_Core_Exception' with message 'Invalid block type: Mage_Alanstormdotcom_Helloworld_Block_Hello' in /var/www/magpractice/app/Mage.php:594
Stack trace:
#0 /var/www/magpractice/app/code/core/Mage/Core/Model/Layout.php(495): Mage::throwException('Invalid block t...')
#1 /var/www/magpractice/app/code/core/Mage/Core/Model/Layout.php(437): Mage_Core_Model_Layout->_getBlockInstance('alanstormdotcom...', Array)
#2 /var/www/magpractice/app/code/local/Alanstormdotcom/Helloworld/controllers/IndexController.php(38): Mage_Core_Model_Layout->createBlock('alanstormdotcom...', 'root')
#3 /var/www/magpractice/app/code/core/Mage/Core/Controller/Varien/Action.php(419): Alanstormdotcom_Helloworld_IndexController->layoutAction()
#4 /var/www/magpractice/app/code/core/Mage/Core/Controller/Varien/Router/Standard.php(250): Mage_Core_Controller_Varien_Action->dispatch('layout')
#5 /var/www/magpractice/app/code/core/Mage/Core/Controller/Varien/Front.php(176): Mage_Core_Controller_Varien_Router_Standard->match(Object(Mage_Core_Controller_Request_Http))
#6 /var/www/magpractice/app/code/core/Mage/Core/Model/App.php(354): Mage_Core_Controller_Varien_Front->dispatch()
#7 /var/www/magpractice/app/Mage.php(683): Mage_Core_Model_App->run(Array)
#8 /var/www/magpractice/index.php(88): Mage::run('', 'store')
#9 {main}

私のコードのどこが間違っているか、アラン・ストーム・ノフリルのマジェント・レイアウト・ドキュメント(これは優れた本です)に記載されている内容でmagento 1.7.0.2がどのように逸脱する可能性があるかを親切に指摘できますか?

ありがとう。

4

2 に答える 2

3

config.xmlファイルが不完全です。むしろ次のようになります。

<config>
  <modules>
    <Alanstormdotcom_Helloworld>
      <version>0.1.0</version>
    </Alanstormdotcom_Helloworld>
  </modules>
  <!-- Missing blocks and helpers definition here: -->
  <global>
    <blocks>
        <helloworld>
            <class>Alanstormdotcom_Helloworld_Block</class>
        </helloworld>
    </blocks>
    <helpers>
        <helloworld>
            <class>Alanstormdotcom_Helloworld_Block</class>
        </helloworld>
    </helpers>
  </global>
  <frontend>
    <routers>
      <helloworld>
        <use>standard</use>
        <args>
          <module>Alanstormdotcom_Helloworld</module>
          <frontName>helloworld</frontName>
        </args>
      </helloworld>
    </routers>
  </frontend>
</config>
于 2013-06-11T13:26:01.673 に答える
3

コードに影響を与えるような 1.7.0.2 の違いはありません —config.xmlその<blocks/>セクションがありません。

という名前のブロックをインスタンス化していますalanstormdotcom_helloworld/hello。のグループ名でalanstormdotcom_helloworldあり、クラス名ですhello

blocksただし、モジュールには構成セクションがありません。このセクションがないと、Magento はalanstormdotcom_helloworldグループ内のブロックに使用する PHP クラス名を認識できません。わからないので、ブロックが Mage コアの一部であると想定し、名前を推測します ( Mage_Alanstormdotcom_Helloworld_Block_Hello)。

作業モジュールが<blocks/>セクションを構成する方法を見てください。それはあなたを正しい方向に向けさせるはずです。

私の作業をチェックせずに、構成は次のようになります

<config>
    <!-- ... -->
    <global>
        <!-- ... -->
         <blocks>
            <alanstormdotcom_helloworld>
                <class>Alanstormdotcom_Helloworld_Block</class>
            </alanstormdotcom_helloworld>
        </blocks>
    </global>
</config>

それでも問題が解決しない場合は、Magento がクラス エイリアスからクラス名を作成する場所までコードをトレースしてみてください ( alanstormdotcom_helloworld/hello)。夜の問題を解決するだけでなく、これは新しい Magento プログラマーにとって常に良い練習になります。

于 2013-06-11T13:47:22.467 に答える