php - Magento copy controller to local

544

When I copy somecore/Mage/Adminhtml/Block/ files tolocal/Mage/Adminhtml/Block/ everything work's fine, but if I copy controller tolocal/Mage/Adminhtml/controllers - core controller still works and system don't see my local/ ... controller.

973

Answer

Solution:

It's because controllers are loaded in a different way than other type classes. You don't do it by referencing toMage class. To know how they are loaded lets look at a standard Magento router and its methodgetControllerFileName():


// Mage_Core_Controller_Varien_Router_Standard
public function getControllerFileName($realModule, $controller)
{
    $parts = explode('_', $realModule);
    $realModule = implode('_', array_splice($parts, 0, 2));
    $file = Mage::getModuleDir('controllers', $realModule);
    if (count($parts)) {
        $file .= DS . implode(DS, $parts);
    }
    $file .= DS.uc_words($controller, DS).'Controller.php';
    return $file;
}

Then lets look atMage_Core_Model_Config::getModuleDir() method (config class is referenced insideMage::getModuleDir()):


public function getModuleDir($type, $moduleName)
{
    $codePool = (string)$this->getModuleConfig($moduleName)->codePool;
    $dir = $this->getOptions()->getCodeDir().DS.$codePool.DS.uc_words($moduleName, DS);

    (...)
}

As you can see, Magento get real module code pool in this case. That's why simple copy won't work. You have to rewrite controllers.

If you don't know how to properly rewrite a controller let me know in the comments. I will update this answer accordingly.

EDIT

To rewrite a controller you need to create a new module in local code pool (or use existing one). If you don't know how to create a module check this SO topic. Lets assume that you want to make a rewrite of Magento Onepage checkout controller.

In config.xml of a module add this:


<frontend>
    <routers>
        <checkout>
            <args>
                <modules>
                    <mynamespace_mymodule before="Mage_Checkout">MyNamespace_MyModule</mynamespace_mymodule>
                </modules>
            </args>
        </checkout>
    </routers>
</frontend>
<!-- rewrite of admin controllers are the same. instead of <frontend> use <admin> -->

Next, create a controller in your module in app/code/local/MyNamespace/MyModule/controllers. Name it the same as controller that is being rewritten. You must use the same folder structure as Magento use. In our case we create aOnepageController.php in controllers folder.


//you have to manually include the controller being rewritten, because Magento autoloader cannot automatically resolve it.
require_once Mage::getModuleDir('controllers', 'Mage_Checkout') . DS . 'OnepageController.php';

class MyNamespace_MyModule_OnepageController extends Mage_Checkout_OnepageController
{
    //copy a method you want to rewrite, e.g.
    public function saveOrderAction()
    {
         //Method body
    }
}

And that's it! Note that you need to flush Magento cache after doing this. Admin controllers are very similar to rewrite. Just change a<frontend> to<admin> and you're good to go. Usually it's a good practice to place admin controllers toAdminhtml subfolder ofcontrollers folder. So example config.xml would look like this:


<admin>
    <routers>
        <adminhtml>
            <args>
                <modules>
                    <mynamespace_mymodule before="Mage_Checkout">MyNamespace_MyModule_Adminhtml</mynamespace_mymodule>
                </modules>
            </args>
        </adminhtml>
    </routers>
</admin>

Note that controller path has changed toMyNamespace_MyModule_Adminhtml but it does not containcontrollers.

Magento will automatically look to controllers with the same relative path and controller name.

People are also looking for solutions to the problem: html - file_get_contents for a specific ( a tag ) from external website URL - PHP

Source

Didn't find the answer?

Our community is visited by hundreds of web development professionals every day. Ask your question and get a quick answer for free.

Ask a Question

Write quick answer

Do you know the answer to this question? Write a quick response to it. With your help, we will make our community stronger.

Similar questions

Find the answer in similar questions on our website.