Posting in the Magento forums has been disabled pending the implementation of a new and improved forum solution which should better serve the community.

For new questions please post at magento.stackexchange.com, the community-run support site for the Magento community. We will be providing updates on the new forum solution soon. For questions or concerns please email community@magento.com.

Magento Forum

Page 1 of 2
How to customize Magento with local (or community) code? 
 
Brikou Carré
Member
 
Avatar
Total Posts:  37
Joined:  2007-10-04
 

I’d like to customize Meganto in order to fit my needs, I’ve notice that /app/code/local/ folder was made for this purpose but when I want to overwrite core classes in my local folder, nothing change. (I know overwritting is not he best way way, overloading would be better).

I took a look at Mage.php file and it seems to me that the order of folders sets in “include_path” disallow this feature.

<?php
// [...]

// from core to local
/*
ini_set('include_path', ini_get('include_path')
    .PS.BP.'/lib'
    .PS.BP.'/app/code/core'
    .PS.BP.'/app/code/local'
    .PS.BP.'/app/code/community'
);
*/

// from local to core
ini_set('include_path'ini_get('include_path')
    .
PS.BP.'/lib'
    
.PS.BP.'/app/code/local'
    
.PS.BP.'/app/code/community'
    
.PS.BP.'/app/code/core'
);

// [...]
?>

Once I change the order (from local to core) everything seems to work… Do you think this is the right way to customize Magento? I mean for overriding this seems correct but how should I do to overload classes (which is probably cleaner)...?

 
Magento Community Magento Community
Magento Community
Magento Community
 
Moshe
Magento Team
 
Avatar
Total Posts:  1770
Joined:  2007-08-07
Los Angeles
 

@Brikou Carré: this is one way of overwriting the functionality. However, If you have more than one customization for same class, it would require much more intervention of developer to make them work together than it is needed.

Magento introduces different approach.

As you may have noticed, all the instantiations in Magento made in style

$instance Mage::getModel('module_name/class_path');

Magento knows how to get the right class for ‘module_name/class_path’ from its modules configuration. So if you have a module with following in config.xml:

<config>
  <global>
    <
models>
      <
module_name>
        <class>
Mage_ModuleName_Model</class>
Magento will know to instantiate Mage_ModuleName_Model_Class_Path model, wherever it would be in include_path.

So how do we implement rewrite functionality?

We create a new module, that contains only this customization or all the customizations you want to group (for example all custom code for your store). and have this in it’s configuration:

<config>
  <global>
    <
models>
      <
core>
        <
rewrite>
          <
url_rewrite>Mage_ModuleName_Model_Url_Rewrite</url_rewrite>
Mage_ModuleName_Model_Url_Rewrite extends Mage_Core_Model_Url_Rewrite and has ONLY methods that needs to be overloaded.
Now, every time Magento requests Mage::getModel(’core/url_rewrite’) it will instantiate class Mage_ModuleName_Model_Url_Rewrite instead of the original.

So how does it help me, if I have 2 or more customizations that overload the same class?

If you do not have a case when 2 customizations overload the same method, it’s fairly simple, you will have just to update the parent extend name in classes so overload chain will be created.
If you have same method overloading, you will have to add parent::methodName() in further customization, depending on the logic.

I hope this answers your question, if you still have unclear moments, ask away smile

 
Magento Community Magento Community
Magento Community
Magento Community
 
Brikou Carré
Member
 
Avatar
Total Posts:  37
Joined:  2007-10-04
 

Thank you Moshe, it’s more clear in my mind. Furthermore, I tried to add a new payment module (as describe in the wiki) and I understood how Magento + local.xml can be helpfull with module creation or class overloading as you explained upper.

Thanks again, once I have some times, I’ll use you method (the clean one) and give up mine with patches (based on my diff command)…

 
Magento Community Magento Community
Magento Community
Magento Community
 
SeL_
Magento Team
 
Avatar
Total Posts:  1140
Joined:  2007-10-10
Paris, France
 

Hi Moshe,

Thanks for these explanations.

Is it possible to overload a Helper?
I gave it a few tries but without success:

<global>
        <
helpers>
            <
catalog>
                <
rewrite>
                    <
product>Mage_NewModule_Helper_Product</product>
                </
rewrite>
            </
catalog>

(Mage_NewModule_Helper_Product extends Mage_Catalog_Helper_Product)

I’d like Magento to call my Helper methods instead of core’s one when calling Mage::getHelper(’catalog/product’).

I can call my method by replacing Mage::getHelper(’catalog/product’) with Mage::getHelper(’newmodule/product’). But i’d like to avoid replacing all the calls in phtml files.
What do you think?
Thanks in advance.

 
Magento Community Magento Community
Magento Community
Magento Community
 
Moshe
Magento Team
 
Avatar
Total Posts:  1770
Joined:  2007-08-07
Los Angeles
 

Mage::getHelper() is using blocks classes as singletons, so you would do <blocks> instead of <helpers> in configuration, and use Mage_Module_Block as a root for custom class.

It supposed to be deprecated soon, as was an early attempt to have abstract representation logic, and later was moved to Mage::helper(), which uses Mage_Module_Helper as root and can contain not only representation, but data structures implementation too.

Currently Mage::helper() can’t be overloaded, but will be in one of the next releases.

 
Magento Community Magento Community
Magento Community
Magento Community
 
seldon
Sr. Member
 
Total Posts:  92
Joined:  2007-11-08
 

Sorry, but I still have troubles understanding how to overload a class. I want to make some modifications in Mage_Catalog_Block_Navigation,
so i created my own: MyOwn_Catalog_Block_Navigation class which overrides the necessary methods. Now where should i put this class? (i figured perhaps in a /app/code/local/category/block/ dir? And how do i make sure this class is used instead of the default one. I do not follow the remarks made eralier in this thread to be honest. Which config file are they talking about? And (if) where/how _exactly_ should i modify it?

I hope anyone can help out!

Thanks,

Seldon

 
Magento Community Magento Community
Magento Community
Magento Community
 
seldon
Sr. Member
 
Total Posts:  92
Joined:  2007-11-08
 

Anyone smile ?

 
Magento Community Magento Community
Magento Community
Magento Community
 
SeL_
Magento Team
 
Avatar
Total Posts:  1140
Joined:  2007-10-10
Paris, France
 

If you write your own module (let’s say Mage_YourOwnModule), you need to declare the module, so you need to modify the following file:
/app/etc/local.xml (because it’s a local module)
by adding in <modules>
the following code:

<Mage_YourOwnModule>
            <
active>true</active>
            <
codePool>local</codePool>
        <
Mage_YourOwnModule>
And then, you put your code in the following folder:
/app/code/local/Mage/YourOwnModule/
So you should have at least a file Block/Navigation.php with your class.
Your Mage_YourOwnModule_Block_Navigation class must extend Mage_Catalog_Block_Navigation class.

To tell Magento to use Mage_YourOwnModule_Block_Navigation instead of Mage_Catalog_Block_Navigation, you need a etc/config.xml file with:

<?xml version="1.0"?>
<config>
    <
modules>
        <
Mage_YourOwnModule>
            <
version>0.1.0</version>
        </
Mage_YourOwnModule>
    </
modules>

    <global>
    <
blocks>
      <
catalog>
        <
rewrite>
          <
navigation>Mage_YourOwnModule_Block_Navigation</navigation>
        </
rewrite>
      </
catalog>
    </
blocks>

    </global>
</
config>

Try this and tell us if this worked out. I hope this is clearer now.

 
Magento Community Magento Community
Magento Community
Magento Community
 
seldon
Sr. Member
 
Total Posts:  92
Joined:  2007-11-08
 

SeL: excellent explanation! It fully works now! Many thnx!!

 
Magento Community Magento Community
Magento Community
Magento Community
 
Brikou Carré
Member
 
Avatar
Total Posts:  37
Joined:  2007-10-04
 

You just ask about overloading helper, but I’m also wondering myself if and how it is possible to overload controller stuff??

Does anyone know?

 
Magento Community Magento Community
Magento Community
Magento Community
 
Moshe
Magento Team
 
Avatar
Total Posts:  1770
Joined:  2007-08-07
Los Angeles
 

@Brikou Carré: including older releases, there was this way:
in module’s config.xml add this in <global> tag:

<rewrite>
  <
my_rewrite>
    <
from><![CDATA[#^/testing/rewrite$#]]></from>
    
<to>/checkout/cart</to>
  </
my_rewrite>
</
rewrite>
<from> contains regular expression of request path fragment you wish to rewrite, enclosed in regex delimiters and can be appended with preg_replace modifiers.

Starting with 0.6.13700 you can use db table core_url_rewrite for dynamic rewrites:

insert into core_url_rewrite (store_idid_pathrequest_pathtarget_path
values (0'my_rewrite/1''somemodule/controller''anothermodule/controller');
store_id is 0 for all stores or store ID for specific store.
id_path is your personal identifier for the URL so you can find it if you wish to create URLs dynamically.
 
Magento Community Magento Community
Magento Community
Magento Community
 
Brikou Carré
Member
 
Avatar
Total Posts:  37
Joined:  2007-10-04
 

Thank you moshe!! smile

 
Magento Community Magento Community
Magento Community
Magento Community
 
seldon
Sr. Member
 
Total Posts:  92
Joined:  2007-11-08
 

Could anyone give an example on how a helper override would work with this method?

 
Magento Community Magento Community
Magento Community
Magento Community
 
Moshe
Magento Team
 
Avatar
Total Posts:  1770
Joined:  2007-08-07
Los Angeles
 

@seldon: http://www.magentocommerce.com/boards/viewreply/6976/

 
Magento Community Magento Community
Magento Community
Magento Community
 
SeL_
Magento Team
 
Avatar
Total Posts:  1140
Joined:  2007-10-10
Paris, France
 

I tried Moshe’s solution to overload a controller.
My own controller must extend Mage_Core_Controller_Front_Action and cannot extend the controler class it overloads, right ?
When i try to overload checkout/cart controller by extending Mage_Checkout_CartController, i get the following error:

Warning: include(Mage/Checkout/CartController.php[function.include]failed to open streamNo such file or directory in /home/seb/magento/app/code/core/Mage/Core/functions.php on line 41
[0] in __autoload
() in /home/seb/magento/app/code/core/Mage/Core/functions.php on line 41
[1] in __autoload
("Mage_Checkout_CartController"in /home/seb/magento/app/code/local/Mage/MyModule/controllers/CartController.php on line 3
[2] in 
include("/home/seb/magento/app/code/local/Mage/MyModule/controllers/CartController."in /home/seb/magento/app/code/core/Mage/Core/Controller/Varien/Router/Standard.php on line 124
[3] in Mage_Core_Controller_Varien_Router_Standard
->match(Zend_Controller_Request_Httpin /home/seb/magento/app/code/core/Mage/Core/Controller/Varien/Front.php on line 166
[4] in Mage_Core_Controller_Varien_Front
->dispatch() in /home/seb/magento/app/Mage.php on line 380
[5] in Mage
::run("base"in /home/seb/magento/index.php on line 28

Warning
: include() [function.include]Failed opening 'Mage/Checkout/CartController.php' for inclusion (include_path='.:/usr/share/php:/usr/share/pear:/home/seb/magento/lib:/home/seb/magento/app/code/core:/home/seb/magento/app/code/local:/home/seb/magento/app/code/community'in /home/seb/magento/app/code/core/Mage/Core/functions.php on line 41
[0] in __autoload
() in /home/seb/magento/app/code/core/Mage/Core/functions.php on line 41
[1] in __autoload
("Mage_Checkout_CartController"in /home/seb/magento/app/code/local/Mage/MyModule/controllers/CartController.php on line 3
[2] in 
include("/home/seb/magento/app/code/local/Mage/MyModule/controllers/CartController."in /home/seb/magento/app/code/core/Mage/Core/Controller/Varien/Router/Standard.php on line 124
[3] in Mage_Core_Controller_Varien_Router_Standard
->match(Zend_Controller_Request_Httpin /home/seb/magento/app/code/core/Mage/Core/Controller/Varien/Front.php on line 166
[4] in Mage_Core_Controller_Varien_Front
->dispatch() in /home/seb/magento/app/Mage.php on line 380
[5] in Mage
::run("base"in /home/seb/magento/index.php on line 28


Fatal error
: Class 'Mage_Checkout_CartController' not found in /home/seb/magento/app/code/local/Mage/MyModule/controllers/CartController.php on line 3

Am i missing something or do i need to rewrite all checkout/cart methods? Isn’t it possible to overload only methods i need to rewrite?
Thanks in advance.

 
Magento Community Magento Community
Magento Community
Magento Community
 
Moshe
Magento Team
 
Avatar
Total Posts:  1770
Joined:  2007-08-07
Los Angeles
 

The problem here is that controllers are not loaded through __autoload. If you wish to overload a controller you need explicitly add

require_once 'Mage/Checkout/controllers/CartController.php';

But overloading controller class is not necessary i think, because controller rewrite works by calling specific action method, without dependancy on other actions.

 
Magento Community Magento Community
Magento Community
Magento Community
Magento Community
Magento Community
Back to top
Page 1 of 2