Saturday, January 19, 2013

Magento - How to switch Base URLs without modifying the Database

As Magento keeps its base URL in database, there is no easy way of accessing the same database from two different URLs(dev/ staging) or moving the same database to staging environment from development without updating core_config_data table.

After fiddling around with Magento core, I found that getBaseUrl() is responsible of retrieving base URL. So If we could override getBaseUrl() method to get the base URL from dev or staging environments without depending on the database, then we could keep database records with live settings.

Below code will explain how I achieved this task and the code itself is explainable.

1. app/etc/modules/My_Configurator.xml

    
        
            true
            local
        
    


2. app/code/local/My/Configurator/etc/config.xml

    
        
            
                
                    My_Configurator_Model_Store
                
            
        
    


3. app/code/local/My/Configurator/Model/Store.php


Note: Make sure you disable this extension from production server as we want to run getBaseUrl() method from the Core.

Happy Coding..