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.
Answer
Solution:
It's because controllers are loaded in a different way than other type classes. You don't do it by referencing to
Mage
class. To know how they are loaded lets look at a standard Magento router and its methodgetControllerFileName()
:Then lets look at
Mage_Core_Model_Config::getModuleDir()
method (config class is referenced insideMage::getModuleDir()
):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:
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 a
OnepageController.php
in controllers folder.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:Note that controller path has changed to
MyNamespace_MyModule_Adminhtml
but it does not containcontrollers
.Magento will automatically look to controllers with the same relative path and controller name.