0

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';
3

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.

1
  • Your response appears most accurate, it is a shame but then again I suppose there is no further logical way of doing the inclusion now that I think about it. – ash Jan 7 '14 at 14:04
0

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

}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.