Magento CMS controller override / cms page controller override in magento

Whoops !! what Magento’s bad ? Ofcourse documentation. Bullshit documentation !!. Magento have features but without documentation !!!!

Magento cms module has some nice functionality. Using Magento’s 404 event handlers of cms module you can integrate 3rd Party CMS Content such Expression Engine, Drupal, wordpress, joomla etc within Magento.
Magento is robust and have some great nice features but still their documentation is very poor. In Magento forum and wiki they just demonstrate “how to override a controller?” as a general theory. But how to override exceptional cms and api controller is not clear. Even Magento did not mention anything about exceptionality of cms and api module. Cms module calls internally. So we cannot override cms module’s controller using as usual magento controller override process. Here I described how to overrite cms index and page controller.

Target : Override Cms module’s Index (Mage_Cms_IndexController) controller and Page (Mage_Cms_IndexController) controller.

Hint: IndexController is responsible for displaying errors (404) & home page and PageController is responsible for displaying pages like ‘about-us’, keep this in mind as you may want to rewrite which controller.

Ok, let’s start—

Firsty, it’s wise to name your module differently to avoid name collisions. Lets call my module M4U_Mycms for that case.

<?xml version="1.0"?>
<config>
    <modules>
        <M4U_Mycms>
            <active>true</active>
            <codePool>local</codePool>
        </M4U_Mycms>
    </modules>
</config>

Now create config xml for new module in app/code/local/M4U/Mycms/etc/config.xml. Here as example I mention xml to override norouteAction method of IndexController.

<?xml version="1.0"?>
<config>

    <modules>
        <M4U_Mycms>
            <version>0.1.0</version>
        </ M4U_Mycms>
    </modules>
   
    <frontend>
        <routers>
            <mycms>
                <use>standard</use>
                <args>
                    <module> M4U_Mycms</module>
                    <frontName>mycms</frontName>
                </args>
            </mycms>
        </routers>
</frontend>	
	
<global>
     <routers>
            <cms>
                <rewrite>
                    <index>
                        <to>M4U_Mycms/index</to>
                        <override_actions>true</override_actions>
                        <actions>
                           <noroute><to> M4U_Mycms/index/noroute</to></noroute>
                        </actions>
                    </index>
                </rewrite>
            </cms>
        </routers>

       </global>
</config>

N.B: Please take care about tag. It will let you create additional NEW actions that will be rewritten to new controller.

Now create new index controller in app/code/local/M4U/Mycms/controllers/IndexController.php and update noRouteAction method.


require_once "Mage/Cms/controllers/IndexController.php";
class M4U_Mycms_IndexController extends Mage_Cms_IndexController
{
    public function noRouteAction($coreRoute = null)
    {
        //modify this method as you need
        
        exit('Yes, I can successfully override cms/index noRoute method ');
		
        //$this->getResponse()->setHeader('HTTP/1.1','404 Not Found');
        //$this->getResponse()->setHeader('Status','404 File not found');
       // $pageId = Mage::getStoreConfig('web/default/cms_no_route');
       // if (!Mage::helper('cms/page')->renderPage($this, $pageId)) {
       //     $this->_forward('defaultNoRoute');
       // }
    }
}

Done !!

When any requested class/method is not found in system then Magento throw “404 Not Found” error using noRouteAction method. So we can test this method calling any undefined page url like http://127.0.0.1/magento/index.php/undefinedpagename. Here, undefinedpagename is not a defined page in magento.

ok, now run http://127.0.0.1/magento/index.php/undefinedpagename and if you get result
“Yes, I can successfully override cms/index noRoute method” in browser then noRouteAction method override is success.

Here i just describe noRouteAction method modification system. Like this way you can modify all of the indexController and pageController method.

You will get source code in right side box widget. Download code and enjoy your work.

if need any help let me know.

About these ads

11 responses to “Magento CMS controller override / cms page controller override in magento

  1. For those looking for the main wiki article on overriding a core Magento controller:
    http://www.magentocommerce.com/wiki/how_to_overload_a_controller

    • Phill, did you ever try to override cms index controller? I am sure you did never. Cms controller override system is not same as other module’s controller. I already override customer, address, cart, admin, catalog, product etc controller by following wiki rule. But cms controller’s functionality is different. Hope you understand. If you have time please try to override cms controller and let me know. Hope this post will help you. I am practical :)

  2. Hi! I need to override AdminHtml IndexController in Magento 1.4.0.1.

    So I downloaded and installed your example about CMS noRoute. However nothing happens: no override is done and Magento function the default way.

    What am I doing wrong?
    Thanks!

  3. Your code rocks, man! I was not working as I had placed the code in the wrong folder: M4U instead of Click2Mail! Now I will try to apply the principles highlighted in your code for my overring the adminhtml controller!

    Thank you so much!!

  4. Hi Moinul,

    Thanks your helpful articles this article and your “Magento Controller Override Rules” are pretty much the only Magento controller override articles on the net (besides the wiki page).

    And I can’t find any article for overriding CMS module controller other than yours. Excellent job!

    A question though, I used your steps successfully in my module (http://github.com/ceefour/magento_tweaks).
    However I don’t see any difference when ‘override_actions=true’ and ‘actions’ element is used. I can just comment/remove those elements and my override still works as before.

    Can you explain what those are required for? Thank you.

  5. Hi Moinul,

    I finally figured why I can’t post to your comment form.

    For some reason, when I put this URL:

    http://www.Soluvas.com/ecommerce-online-store

    in the Website field, I can’t comment. There’s no error message or whatever, just a blank page.

    But if I use this url as my Website:

    http://www.Soluvas.com/

    I can comment.

    I think something needs fixing at your WordPress blog. Good luck.

  6. basant choudhary

    Hi,

    I tired to replicate you code into my newly installed magento but its not working. I also tried to download and install your files (to rule out typo errors) but its still didnt work. I am using magento 1.4.1.1 . Does it still work or is there any changes with later versions? Please let me know.

  7. Hi, it didn’t work for me neither on contacts.

  8. This is exactly what I was looking for! Thanks so much!

  9. This does not work on magento 1.5. After creating all the files and uploading them on ftp, nothing happens on 404 – it is still default one.

  10. I do this and received a problem:

    Front controller reached 100 router match iterations

    You know how fix it?

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s