我想在CatalogSearch Core模块中添加名为SkuController的附加控制器。 这样做的最佳做法是什么?
感谢。
答案 0 :(得分:1)
无论您做什么,都不要修改核心,而是创建自定义模块,并确保始终尽可能调用父函数。
使用ModuleCreater简化此任务: http://www.magentocommerce.com/magento-connect/modulecreator.html
看看Ivan的视频教程,只是忽略了php单元测试位,但他解释了很多,特别是在视频的一半时间。
另请查看此示例以获取更多想法: http://www.magentocommerce.com/knowledge-base/entry/magento-for-dev-part-3-magento-controller-dispatch
在两个链接上分享了良好的最佳实践想法。
控制器类可能如下所示:
class Mycompany_myMod_Adminhtml_myModController extends Mage_Adminhtml_Controller_action
{
protected function _initAction() {
$this->loadLayout()
->_setActiveMenu('custompromos/items')
->_addBreadcrumb(Mage::helper('adminhtml')->__('Items Manager'), Mage::helper('adminhtml')->__('Item Manager'));
return $this;
}
public function indexAction() {
$this->_initAction()
->renderLayout();
}
public function editAction() {
$id = $this->getRequest()->getParam('id');
//Some code here
}
public function newAction() {
$this->_forward('edit');
}
public function saveAction() {
if ($data = $this->getRequest()->getPost()) {
//Some code here
}
}
public function deleteAction() {
if( $this->getRequest()->getParam('id') > 0 ) {
try {
//Some code here
} catch (Exception $e) {
Mage::getSingleton('adminhtml/session')->addError($e->getMessage());
$this->_redirect('*/*/edit', array('id' => $this->getRequest()->getParam('id')));
}
}
$this->_redirect('*/*/');
}
public function massDeleteAction() {
//Some code here
$this->_redirect('*/*/index');
}
public function massStatusAction()
{
//Some code here
$this->_redirect('*/*/index');
}
public function exportCsvAction()
{
$fileName = 'somedata.csv';
$content = $this->getLayout()->createBlock('mymodule/adminhtml_mymodule_grid')
->getCsv();
$this->_sendUploadResponse($fileName, $content);
}
public function exportXmlAction()
{
$fileName = 'somedata.xml';
$content = $this->getLayout()->createBlock('mymodule/adminhtml_mymodule_grid')
->getXml();
$this->_sendUploadResponse($fileName, $content);
}
protected function _sendUploadResponse($fileName, $content, $contentType='application/octet-stream')
{
$response = $this->getResponse();
$response->setHeader('HTTP/1.1 200 OK','');
$response->setHeader('Pragma', 'public', true);
$response->setHeader('Cache-Control', 'must-revalidate, post-check=0, pre-check=0', true);
$response->setHeader('Content-Disposition', 'attachment; filename='.$fileName);
$response->setHeader('Last-Modified', date('r'));
$response->setHeader('Accept-Ranges', 'bytes');
$response->setHeader('Content-Length', strlen($content));
$response->setHeader('Content-type', $contentType);
$response->setBody($content);
$response->sendResponse();
die;
}
}
答案 1 :(得分:1)
所有额外功能都应由模块完成。我建议使用新控制器制作一个新模块。