我是Magento的新手,我需要在Magento中创建一个自定义模块。 我尝试了以下代码但没有成功。 那有什么解决方案吗?请提供给我并分享一些书籍链接和Youtube链接。
代码文件为
应用程序/代码/本地
-Baobaz
-Arithmetic
它包含块,控制器等,Model。
在controllers
文件夹中有一个名为的控制器文件
IntegerController.php和这里的代码:
<?php
class Baobaz_Arithmetic_IntegerController extends Mage_Core_Controller_Front_Action
{
public function multiplyAction(){
$this->loadLayout();
$this->renderLayout();
}
}
配置文件:
<?xml version="1.0" encoding="UTF-8"?>
<config>
<modules>
<baobaz_arithmetic>
<version>0.0.1</version>
</baobaz_arithmetic>
</modules>
<frontend>
<routers>
<arithmetic>
<use>standard</use>
<args>
<module>Baobaz_Arithmetic</module>
<frontName>arithmetic</frontName>
</args>
</arithmetic>
</routers>
<layout>
<updates>
<arithmetic>
<file>arithmetic.xml</file>
</arithmetic>
</updates>
</layout>
</frontend>
<global>
<blocks>
<arithmetic>
<class>Baobaz_arithmetic_Block</class>
</arithmetic>
</blocks>
<models>
<arithmetic>
<class>Baobaz_arithmetic_Model</class>
</arithmetic>
</models>
</global>
</config>
Model/Arithmetic.php
代码:
<?php
class Baobaz_Arithmetic_Model_Arithmetic extends Mage_Core_Model_Abstract {
public function getRecentProducts() {
$products = Mage::getModel("catalog/product")
->getCollection()
->addAttributeToSelect('*')
->setOrder('entity_id', 'DESC')
->setPageSize(5);
return $products;
}
}
Block/Arithmetic.php
代码:
<?php
class Baobaz_Arithmetic_Block_Arithmetic extends Mage_Core_Block_Template {
public function getRecentProducts() {
// call model to fetch data
$arr_products = array();
$products = Mage::getModel('arithmetic/arithmetic')->getRecentProducts();
foreach ($products as $product) {
$arr_products[] = array(
'id' => $product->getId(),
'name' => $product->getName(),
'url' => $product->getProductUrl(),
);
}
return $arr_products;
}
}
app/design/frontend/default/default/layout/arithmetic.xml
:
<?xml version="1.0" encoding="UTF-8"?>
<layout version="0.1.0">
<arithmetic_integer_multiply>
<reference name="root">
<action method="setTemplate">
<template>page/1column.phtml</template>
</action>
</reference>
<reference name="content">
<block type="arithmetic/arithmetic" name="arithmetic/arithmetic" template="arithmetic/integer/multiply.phtml"></block>
</reference>
</arithmetic_integer_multiply>
</layout>
arithmetic/integer/multiply.phtml
代码:
<?php
$products = $this->getRecentProducts();
?>
<div id="product_list">
<h1>Recent Products</h1>
<?php if (is_array($products) && count($products)) { ?>
<?php foreach($products as $product) { ?>
<div>
<a href="<?php echo $product['url'] ?>"><?php echo $product['name'] ?></a>
</div>
<?php } ?>
<?php } ?>
</div>
但我收到以下错误:
Notice: Undefined variable: this in /opt/lampp/htdocs/magento/app/design/frontend/default/default/template/arithmetic/integer/multiply.phtml on line 3
#0 /opt/lampp/htdocs/magento/app/design/frontend/default/default/template/arithmetic/integer/multiply.phtml(3): mageCoreErrorHandler(8, 'Undefined varia...', '/opt/lampp/htdo...', 3, Array)
#1 /opt/lampp/htdocs/magento/app/code/core/Mage/Core/Block/Template.php(241): include('/opt/lampp/htdo...')
#2 /opt/lampp/htdocs/magento/app/code/core/Mage/Core/Block/Template.php(272): Mage_Core_Block_Template->fetchView('frontend/defaul...')
#3 /opt/lampp/htdocs/magento/app/code/core/Mage/Core/Block/Template.php(286): Mage_Core_Block_Template->renderView()
#4 /opt/lampp/htdocs/magento/app/code/core/Mage/Core/Block/Abstract.php(886): Mage_Core_Block_Template->_toHtml()
#5 /opt/lampp/htdocs/magento/app/code/core/Mage/Core/Block/Text/List.php(43): Mage_Core_Block_Abstract->toHtml()
#6 /opt/lampp/htdocs/magento/app/code/core/Mage/Core/Block/Abstract.php(886): Mage_Core_Block_Text_List->_toHtml()
#7 /opt/lampp/htdocs/magento/app/code/core/Mage/Core/Block/Abstract.php(605): Mage_Core_Block_Abstract->toHtml()
#8 /opt/lampp/htdocs/magento/app/code/core/Mage/Core/Block/Abstract.php(549): Mage_Core_Block_Abstract->_getChildHtml('content', true)
#9 /opt/lampp/htdocs/magento/app/design/frontend/base/default/template/page/1column.phtml(48): Mage_Core_Block_Abstract->getChildHtml('content')
#10 /opt/lampp/htdocs/magento/app/code/core/Mage/Core/Block/Template.php(241): include('/opt/lampp/htdo...')
#11 /opt/lampp/htdocs/magento/app/code/core/Mage/Core/Block/Template.php(272): Mage_Core_Block_Template->fetchView('frontend/base/d...')
#12 /opt/lampp/htdocs/magento/app/code/core/Mage/Core/Block/Template.php(286): Mage_Core_Block_Template->renderView()
#13 /opt/lampp/htdocs/magento/app/code/core/Mage/Core/Block/Abstract.php(886): Mage_Core_Block_Template->_toHtml()
#14 /opt/lampp/htdocs/magento/app/code/core/Mage/Core/Model/Layout.php(555): Mage_Core_Block_Abstract->toHtml()
#15 /opt/lampp/htdocs/magento/app/code/core/Mage/Core/Controller/Varien/Action.php(390): Mage_Core_Model_Layout->getOutput()
#16 /opt/lampp/htdocs/magento/app/code/local/Baobaz/Arithmetic/controllers/IntegerController.php(6): Mage_Core_Controller_Varien_Action->renderLayout()
#17 /opt/lampp/htdocs/magento/app/code/core/Mage/Core/Controller/Varien/Action.php(418): Baobaz_Arithmetic_IntegerController->multiplyAction()
#18 /opt/lampp/htdocs/magento/app/code/core/Mage/Core/Controller/Varien/Router/Standard.php(250): Mage_Core_Controller_Varien_Action->dispatch('multiply')
#19 /opt/lampp/htdocs/magento/app/code/core/Mage/Core/Controller/Varien/Front.php(172): Mage_Core_Controller_Varien_Router_Standard->match(Object(Mage_Core_Controller_Request_Http))
#20 /opt/lampp/htdocs/magento/app/code/core/Mage/Core/Model/App.php(354): Mage_Core_Controller_Varien_Front->dispatch()
#21 /opt/lampp/htdocs/magento/app/Mage.php(683): Mage_Core_Model_App->run(Array)
#22 /opt/lampp/htdocs/magento/index.php(93): Mage::run('', 'store')
#23 {main}
答案 0 :(得分:0)
当我将上述行(...第3行的... multiply.phtml)复制并粘贴到我的编辑器中时,它会显示&#34; $ this&#34;?后面有一个特殊字符。尝试删除该字符。
products = $this[here]->getRecentProducts();