【问题标题】:why invoke the function from the model in the template file there is no output?为什么从模板文件中的模型调用函数没有输出?
【发布时间】:2013-02-09 16:47:20
【问题描述】:

我是 MVC 和 php 的新手。现在在magento app/code/core/Mage/Review/Model/Review.php

我添加代码。

 public function mgtest(){
        return 'hello world!';
    }

在课堂上

class Mage_Review_Model_Review extends Mage_Core_Model_Abstract
{ }

在magento cms页面,我添加代码:

 {{block type="core/template" name="review" template="catalog/product/reviewall.phtml"}}

url 键是 review.html

然后添加文件reviewall.phtml(app/design/frontend/default/mytheme/template/catalog/product/reviewall.phtml)

然后在其文件中添加测试代码。

<?php echo $this->mgtest();?>

当我访问网址时。 mysiteurl/review.html。没有 hello world 输出。为什么?谢谢。如何纠正它?模型中的所有文件是做什么的?如果我想调用 phtml 文件的函数。我该怎么办?

【问题讨论】:

    标签: php magento


    【解决方案1】:

    不要编辑核心来添加新功能。在app/code/local 目录中添加所有添加/修改。

    在您现有的代码中,我看到两个问题:

    • CMS 页面中的块类型应为review/review 而不是core/template
    • 您已创建函数为matest(),但调用它为mgtest()

    您已从您的 reviewall.phtml 中引用了 $this,调用了/应该位于 Review_Block_Review 而不是 Core_Block_Template 中的 matest 方法,因此您的 CMS 块类型必须是 review/review

    我刚刚看到您在模型中放置了matest 方法,请将其放入 Block,因为您无法使用 $this 直接从模板文件中查看模型。

    一旦您知道如何在 Magento 中完成工作,建议您创建自己的自定义模块(在本地目录中)。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-10-10
      • 1970-01-01
      • 1970-01-01
      • 2017-04-02
      • 2020-05-21
      • 2013-01-17
      相关资源
      最近更新 更多