向 Magento 核心模块添加额外控制器的最佳实践是什么

发布于 2025-01-05 23:56:25 字数 87 浏览 2 评论 0原文

我想将名为 SkuController 的附加控制器添加到 CatalogSearch Core 模块。 执行此操作的最佳实践是什么?

谢谢。

I want to add additional controller named SkuController to CatalogSearch Core module.
What is best practice to do this?

Thanks.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

睡美人的小仙女 2025-01-12 23:56:25

所有额外的功能都应该由模块来实现。我建议用新控制器制作一个新模块。

all extra funcionality should made by module. I recomend made a new module with the new controller.

半暖夏伤 2025-01-12 23:56:25

无论您做什么,都不要修改核心,而是创建自定义模块,并确保始终尽可能调用父函数。

使用 ModuleCreater 来简化此任务:
http://www.magentocommerce.com/magento-connect/modulecreator.html

看看 Ivan 的视频教程,忽略 php 单元测试部分,但他解释了全部内容,尤其是在视频中的一半。

http://vimeo.com/35937480

另请参阅此示例以获取更多想法:
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;
    }
}

Whatever you do, do not modify the core, but create a custom module, and ensure you always call the parent functions where possible.

Use ModuleCreater to simplify this task:
http://www.magentocommerce.com/magento-connect/modulecreator.html

Have a look at Ivan's video tutorial, just ignore the php unit testing bit, but he explains the whole lot, especially half way during the video.

http://vimeo.com/35937480

Also look at this sample for some more ideas:
http://www.magentocommerce.com/knowledge-base/entry/magento-for-dev-part-3-magento-controller-dispatch

Good best practice ideas shared on both links.

A controller class could look like this:

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;
    }
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文