سؤال

Synopsis

I have a question regarding the best practices with Magento dependencies. Essentially I have a custom module that overrides Mage_Adminhtml_Catalog_ProductController this currently works providing I include the Magento core class prior to extending (generally what you'd expect).

My code looks something along the lines of:

<?php

require_once Mage::getModuleDir('controllers', 'Mage_Adminhtml') . DS . 'Catalog/ProductController.php';

class Vendor_Module_Adminhtml_Catalog_ProductController
    extends Mage_Adminhtml_Catalog_ProductController
{
    // My things go here.
}

My question is simply to understand if theres a nicer method of including core classes for overriding (see example)?

Example

require_once Mage::getModuleDir('controllers', 'Mage_Adminhtml') . DS . 'Catalog/ProductController.php';
هل كانت مفيدة؟

المحلول

For Controllers, there is no nicer way unfortunately, you need to use require_once or something like that. You should replace all occurences of / with DS though (one / is still in your sample code).

If you are overriding Models, Blocks or Helpers, you won't need it, as the overwritten classes get autoloaded.

نصائح أخرى

This is an example of overriding a core controller class

In your custom modules config.xml file put the following code.

<config>
        <admin>
            <routers>
                <adminhtml>
                    <args>
                        <modules>
                            <Vendor_Module before="Mage_Adminhtml">Vendor_Module_Adminhtml</Vendor_Module >
                        </modules>
                    </args>
                </adminhtml>
            </routers>
        </admin>
    </config>

Then the your controller class will be like this.

<?php
require_once 'Mage/Adminhtml/controllers/Catalog/ProductController.php';
class Vendor_Module_Adminhtml_Catalog_ProductController
    extends Mage_Adminhtml_Catalog_ProductController
{

// your code

}
مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى magento.stackexchange
scroll top