我正在尝试创建一个自定义控制器的ajax调用。
我一直在关注:http://www.atwix.com/magento/ajax-requests-in-magento/ - 它提供了一个如何创建的简短示例。
所以我有以下文件:
app/etc/moudles/BM_Sidebar.xml
<?xml version="1.0"?>
<config>
<modules>
<BM_Sidebar>
<active>true</active>
<codePool>local</codePool>
</BM_Sidebar>
</modules>
</config>
app/code/local/BM/Sidebar/controllers/IndexController.php
class BM_Sidebar_IndexController extends Mage_Core_Controller_Front_Action {
public function indexAction() {
echo "test data";
}
}
app/code/local/BM/Sidebar/controllers/etc/config.xml
<?xml version="1.0"?>
<config>
<modules>
<BM_Sidebar>
<version>0.1.0</version>
</BM_Sidebar>
</modules>
<frontend>
<routers>
<sidebar>
<use>standard</use>
<args>
<module>BM_Sidebar</module>
<frontName>carfilter</frontName>
</args>
</sidebar>
</routers>
<layout>
<updates>
<sidebar>
<file>sidebar.xml</file>
</sidebar>
</updates>
</layout>
</frontend>
</config>
我正在努力弄清楚我需要投入sidebar.xml
我是否需要创建一个块类?
由于
答案 0 :(得分:15)
它总是从config.xml开始:
声明您的路由器:使用与frontName
标记内容相同的路由器名称
<frontend>
<routers>
<carfilter>
<use>standard</use>
<args>
<module>BM_Sidebar</module>
<frontName>carfilter</frontName>
</args>
</carfilter>
</routers>
</frontend>
声明你的布局文件(你做到了)
需要2个句柄:1表示init状态,1表示ajax。句柄与您正在使用的URL匹配:
<layout version="0.1.0">
<carfilter_ajax_index>
<reference name="head">
<action method="addItem"><type>skin_js</type><name>js/carfilter.js</name></action>
</reference>
<reference name="content">
<block type="core/template" name="carfilter" as="carfilter" template="carfilter/init.phtml" />
</reference>
</carfilter_ajax_index>
<carfilter_ajax_ajax>
<remove name="right"/>
<remove name="left"/>
<block type="core/template" name="carfilter_ajax" as="carfilter_ajax" template="carfilter/ajax.phtml" output="toHtml" />
</carfilter_ajax_ajax>
</layout>
注意:注意AJAX调用块声明中的output
属性
创建您的phtml文件(您在布局文件中声明的文件):
init.phtml:创建将使用AJAX结果更新的div并启动javascript对象
first state
<div id="div-to-update"></div>
<script type="text/javascript">
//<![CDATA[
new Carfilter('<?php echo $this->getUrl('carfilter/ajax/ajax') ?>', 'div-to-update');
//]]>
</script>
ajax.phtml:你想用AJAX显示的html
var Carfilter = Class.create();
Carfilter.prototype = {
initialize: function(ajaxCallUrl, divToUpdate) {
this.url = ajaxCallUrl;
this.div = divToUpdate;
this.makeAjaxCall();
},
makeAjaxCall: function() {
new Ajax.Request(this.url, {
onSuccess: function(transport) {
var response = transport.responseText.evalJSON();
$(this.div).update(response.outputHtml);
}.bind(this)
});
}
};
控制器:本例中的2个动作,页面加载时的索引和ajax:
<?php
class BM_Sidebar_AjaxController extends Mage_Core_Controller_Front_Action
{
public function indexAction()
{
$this->loadLayout();
$this->_initLayoutMessages('customer/session');
$this->getLayout()->getBlock('head')->setTitle($this->__('Page title'));
$this->renderLayout();
}
public function ajaxAction()
{
$isAjax = Mage::app()->getRequest()->isAjax();
if ($isAjax) {
$layout = $this->getLayout();
$update = $layout->getUpdate();
$update->load('carfilter_ajax_ajax'); //load the layout you defined in layout xml file
$layout->generateXml();
$layout->generateBlocks();
$output = $layout->getOutput();
$this->getResponse()->setHeader('Content-type', 'application/json');
$this->getResponse()->setBody(Mage::helper('core')->jsonEncode(array('outputHtml' => $output)));
}
}
}
为了回答你的问题,你不一定需要创建自己的块(在我的例子中我没有),但你可能希望在一个方便的地方拥有模板文件中所需的功能< / p>
答案 1 :(得分:-3)
您需要在模板布局目录中创建文件sidebar.xml。这将指向您的控制器。我不能在这里分享整个文件结构。但是可以从下载/创建自定义模块的位置共享。
http://www.webspeaks.in/2010/08/create-your-first-adminbackend-module.html
希望它会对你有所帮助!