Magento Forum

   
Problem to overload cms IndexController
 
tony-fav
Jr. Member
 
Total Posts:  11
Joined:  2008-06-10
France
 

Hi everybody!

I’ve overloaded IndexController for cms pages to control if users are logged in or not
so, they arrived on the home page and are forced to log-in… you understood me
no problem for this part, it works like i wanted

but when i try to access cms pages, like about-us page, customer-service page, etc… I get this exception:

Front controller reached 100 router match iterations
Trace:
#0 /var/www/vhosts/ecommerce.e-quasar.com/httpdocs/sauflon/app/code/core/Mage/Core/Controller/Varien/Front.php(152): Mage::throwException(’Front controlle...’)
#1 /var/www/vhosts/ecommerce.e-quasar.com/httpdocs/sauflon/app/Mage.php(427): Mage_Core_Controller_Varien_Front->dispatch()
#2 /var/www/vhosts/ecommerce.e-quasar.com/httpdocs/sauflon/index.php(40): Mage::run()
#3 {main}

it probably means that the overload is not correct in cms config.xml file:

<config>
    <
modules>
        <
Mymodule_Cms>
            <
version>0.7.6</version>
        </
Mymodule_Cms>
    </
modules
    
    <
frontend>
        <
routers>
         <
cms>
            <use>
standard</use>
            <
args>
               <
module>Mymodule_Cms</module>
               <
frontName>cms</frontName>
            </
args>
         </
cms>
        </
routers>
    </
frontend>
</
config>

i’ve tried the wiki’s method but whithout success because the URL of cms pages already change:
- //www.domain.com/store/about-us
- //www.domain.com/store/customer-service
I’don’t see how i should do…

So i f you have any idea which can help me…

thanks wink

 
Magento Community Magento Community
Magento Community
Magento Community
 
tony-fav
Jr. Member
 
Total Posts:  11
Joined:  2008-06-10
France
 

I cannot simply redirect <index cms page> without crashing other cms pages !!
I think it’s impossible to overload cms controllers

So how can i do to change the default home page ( to “account/login” page) without overloading the indexController ? help me please !!

(Maybe using events & observers, but i’m not sure, i will try it until someone meets me) smile

 
Magento Community Magento Community
Magento Community
Magento Community
 
shoo
Jr. Member
 
Avatar
Total Posts:  10
Joined:  2008-08-26
Poland, Lodz
 

Firsty, its wise to name your module diffrently to avoid name collisions. Lets call your module Mymodule_Test for that case, now try to do the following

In app/etc/modules/Mymodule_Test.xml add something like this:

<config>
    <global>
        <
routers>
            <
cms>
                <
rewrite>
                    <
page>
                        <
to>Mymodule_Test/page</to>
                        <
override_actions>true</override_actions>
                        <
actions>
                            <
NEW_ACTION><to>Mymodule_Test/SOME_CONTROLLER/SOME_ACTION</to></NEW_ACTION>
                        </
actions>
                    </
page>
                </
rewrite>
            </
cms>
        </
routers>
    </global>
</
config>

This will rewrite all actions from PageController in Mage_Cms to your Mymodule_Test/page. Tag override_actions will redirect ALL actions to your new module so its wise to make this controller inherits from oryginal one and there extends parent’s methods. Create

Mymodule_Test/controller/PageController.php and create class

include "Mage/Cms/controllers/PageController.php";

class 
Mymodule_Test_PageController extends Mage_Cms_PageController {
 
// ...
}

<actions></actions> will let you create additional NEW actions that will be rewritten to your controller.

Last thing to consider for you are layouts - this way you will have to create your own layout file, if you do not wish to do it (and you probably dont) in Mymodule_Test_PageController create something like this:

public function preDispatch()
{
  parent
::preDispatch();
  
$this->getRequest()->setRouteName('cms');
  
// here check also for $this->getRequest->getActionName()
  // if this is your own action, make sure to do setRouteName('test') 
  // in order to use your own layout
}

This will force script to use layout for route ‘cms’ (Mage_Cms in this case). Unless you create diffrent frontName, becouse of name collisions you cannot create Mymodule_Cms and rewrite Mage_Cms (unless you use diffrent frontName).

In config.xml for your module set up routing as well

<config>
    <
frontend>
        <
routers>
            <
test>
                <use>
standard</use>
                <
args>
                    <
module>Mymodule_Test</module>
                    <
frontName>test</frontName>
                </
args>
            </
test>
        </
routers>
</
config>

And you will be able to use /test/… urls as well.

I hope this all helps :D I was writing it from memory without testing so I cant promise it will all work fine :D.

Dont forget to set-up your own layout, models and helpers if you require them.

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

 
Magento Community Magento Community
Magento Community
Magento Community
 
tony-fav
Jr. Member
 
Total Posts:  11
Joined:  2008-06-10
France
 

thank you shoo for taking time to try to help me !!

I’ve tested your method but without success, i still think that there is a solution, so i will try few methods until I find one.

Are you sure about your <override_actions> tag ? because i’ve searched in that way to understand, and i didn’t find any information about this tag.

 
Magento Community Magento Community
Magento Community
Magento Community
 
shoo
Jr. Member
 
Avatar
Total Posts:  10
Joined:  2008-08-26
Poland, Lodz
 

Well, if you check source code you can see in file app\code\core\Mage\Core\Controller\Varien\Action.php, at the very bottom.

Its even explained in a comment there, I am successfully using it for my two-step authentication - overriding Mage_Customer (Account Controller).

It took me some time to read source code & debug my rewritten controller but for the time being it works great. About <override_actions/>, from my understanding setting it to TRUE will override all actions that exist in old module to new module, whereas if its is disabled you have to manually decide which actions to rewrite using <actions/> tag. Study the Mage_Core_Controller_Varien_Action for this.

Basically, if you wish to overload IndexController you will have to set-up configs more or less the way I did. Check names for spelling mistakes and dont double module names either. Its all in the details, best way to do it is make your module name/block names/session names/model names totally unique. At first I was using module name MyCompany_FraudProtection, I was using <mycompany> as config id and frontName, in addition I had model MyCompany_FraudProtection_MyCompany and in it I was including external library (from lib/mycompany/ directory) calles MyCompany as well.

And at the top of model I had include “mycompany.php”. Now, believe me, it was total mess and it didn’t work at all :D I switched to unique names and it all started to work smile PM me if you want me to send you my source code (its under development, although rewriting works fine).

 
Magento Community Magento Community
Magento Community
Magento Community
 
tony-fav
Jr. Member
 
Total Posts:  11
Joined:  2008-06-10
France
 

I’ve searched entire forum to find anyone else who meets a similar problem, but though the wiki thread wich override checkout cart controller, nobody try to overload controllers of other modules ( like catalog or cms modules) !!
and this problem is driving me crazy !!

I didn’t find any explanation about <override_actions> tag in Controller/Varien/Action.php, maybe i’ve a different version of Magento ( ver. 1.0.19870.1)

If your solution seems to work, i will try it, but i’m afraid with modify a lot of files to simply redirect users from home page to account/login page…

So I will PM you shoo if your proposition still ok smile
and really thank you for your help

( I still “aware” if someone have a solution to give… )

 
Magento Community Magento Community
Magento Community
Magento Community
 
tony-fav
Jr. Member
 
Total Posts:  11
Joined:  2008-06-10
France
 

OK, I can propose a solution that isn’t really one but it works !!

I keep my config.xml file ( see post 1) with frontend & routers tags wich route (that’s what i believe to have understood) “magento cms module” to “my cms module”
and keeping this in mind, i’ve pasted two cms controllers file ( IndexController.php and PageController.php) in my_module/cms/controllers/ directory

for the IndexController, i’ve just added few lines to redirect users to the account login page (the original purpose), so i’ve created class wich extends mage class ( without forget to include source file ) and and have modfied the indexAction method.

for the PageController, i’ve just created an empty class wich extends the original

in doing so, we can see that the index page redirect to login page and other cms pages don’t give the “100 router match iterations” message.

hoping that this thread will help some people

Regards

 
Magento Community Magento Community
Magento Community
Magento Community
 
dchoi
Jr. Member
 
Total Posts:  3
Joined:  2008-09-09
 

Hey Tony

I followed shoo’s post and it still didn’t work for me either…

But I tried several things, and finally, I got it to work. The main magic word here is ‘unique’. By that I mean you HAVE to name your module with a different name than Cms. (which is the core_module name by default : Mage_Cms) The thing is, you get an infinite loop when you call it the same way. (it defines a frontname, calls your controller with the defined frontname, and that calls your controller with the same frontname again and again....) So you have to have a different frontname. Here is my code :

<global>
        <
routers>
            <
catalog>
                <
rewrite>
                    <
category>
                        <
to>Myown_Catalogue/categorie</to>
                        <
override_actions>true</override_actions>
                        <
actions>
                            <
boom><to>Myown_Catalogue/other_controller_or_categorie/other_action</to></boom>
                        </
actions>
                    </
category>
                </
rewrite>
            </
catalog>
        </
routers>
    </global>
    <
frontend>
        <
routers>
            <
catalogue>
                <use>
standard</use>
                <
args>
                    <
module>Myown_Catalogue</module>
                    <
frontName>catalogue</frontName>
                </
args>
            </
catalogue>
        </
routers>
    </
frontend>

I’ve put Catalogue for where I should put my own module, and catalog for the core module. you don’t need to have new_module/categorie, you can have new_module/category. The least you have to do is name your module differently.

And that’s it ! Happy to help.

David

PS : and by the way, thx a lot for you to have brought up the issue, and for shoo to have answered so well, that helped me alot myself.

 
Magento Community Magento Community
Magento Community
Magento Community
 
novon
Jr. Member
 
Total Posts:  13
Joined:  2008-09-26
 

Hello all. I’m trying to achieve a similar task to the above posts but cannot seem to get my rewritting to work. I am overloading the catalog product controller (in particular the save function). Here is my etc/config.xml

<?xml version="1.0"?>
<config>
    <
modules>
        <
Mockett_Mdeleteimage>
            <
version>0.1.0</version>
        </
Mockett_Mdeleteimage>
    </
modules>
    <global>
        <
routers>
            <
catalog>
                <
rewrite>
                    <
product>
                        <
to>Mockett_Mdeleteimage/save</to>
                        <
override_actions>true</override_actions>
                        <
actions>
                            <
save><to>Mockett_Mdeleteimage/Product_Controller/save</to></save>
                        </
actions>
                    </
product>
                </
rewrite>
            </
catalog>
        </
routers>
    </global>
    <
frontend>
        <
routers>
            <
mockett_mdeleteimage>
                <use>
standard</use>
                <
args>
                    <
module>Mockett_Mdeleteimage</module>
                    <
frontName>mdeleteimage</frontName>
                </
args>
            </
mockett_mdeleteimage>
        </
routers>
    </
frontend>
</
config>

Essentially when index.php/admin/catalog_product/save/id/4574/ is called, it calls the frontend CatalogProduct controller’s save action. My controller now looks like this:

require_once 'Mage/Adminhtml/controllers/Catalog/ProductController.php';
class 
Mockett_Mdeleteimage_Catalog_ProductController extends Mage_Adminhtml_Catalog_ProductController{

    
public function saveAction(){
        
exit('exiting in new controller... woo woo');
        
        
parent::saveAction();
    
}
}

Any help on this would be greatly appreciated!

 
Magento Community Magento Community
Magento Community
Magento Community
 
coooool
Jr. Member
 
Total Posts:  13
Joined:  2008-12-08
 

I am also trying to override Mage/Adminhtml/controllers/Catalog/ProductController.php

Anyone can help???

 
Magento Community Magento Community
Magento Community
Magento Community
 
a-maged
Jr. Member
 
Total Posts:  2
Joined:  2008-11-20
 

check http://www.magentocommerce.com/boards/viewthread/25138/ rasberry

 
Magento Community Magento Community
Magento Community
Magento Community
 
ben_marks
Moderator
 
Avatar
Total Posts:  449
Joined:  2008-10-09
Charleston, SC
 

@shoo - Brilliant, I noticed in a dump of $this (my Catalog_ProductController) that the route was defaulting (?) to ‘core’ rather than catalog.  Creating a preDispatch method that setRouteName to ‘catalog’ allowed my catalog/product_view layout to load.

 
Magento Community Magento Community
Magento Community
Magento Community
 
ben_marks
Moderator
 
Avatar
Total Posts:  449
Joined:  2008-10-09
Charleston, SC
 

As a further note, I tried to make a meta package for our various improvements and used Xxx_Enhance as our structure.  Adjusting the routes from Xxx_Catalog to Xxx_Enhance_Catalog yielded a 100-iteration route match (no route match) error.  Changing to XxxEnhance fixed it all.

 
Magento Community Magento Community
Magento Community
Magento Community
 
chapagain
Sr. Member
 
Avatar
Total Posts:  202
Joined:  2009-07-19
 

I have to override.. saveAction() of class Mage_Adminhtml_Catalog_CategoryController with Mynamespace_Mymodule_CategoryController. How can I do that in Magento 1.3.2.2 ?

I have tried controller override with the following xml.. I think i am wrong on xml ..

I only want to override saveAction() but as I write the following rewrite code.. then indexAction() is searched by magento and 404 Page not found error is displayed.

<global>        
        <
routers>        
            <
adminhtml>
                    <
rewrite>  
                         <
catalog_category>
                             <
to>Mynamespace_Mymodule/category</to>  
                             <
override_actions>true</override_actions>  
                             <
actions>  
                                <
save><to>Mynamespace_Mymodule/category/save</to></save>  
                             </
actions>   
                         </
catalog_category>
                    </
rewrite>
            </
adminhtml>            
        </
routers>            
    </global>
My controller class:

<?php 
require_once "Mage/Adminhtml/controllers/Catalog/CategoryController.php";
class 
Mynamespace_Mymodule_CategoryController extends Mage_Adminhtml_Catalog_CategoryController
{        
    
public function saveAction() 
    

        
// save code goes here
   
}
}

Please help.

 
Magento Community Magento Community
Magento Community
Magento Community
 
marciomaciel
Jr. Member
 
Avatar
Total Posts:  28
Joined:  2009-06-01
Brazil
 

Hi!

I need to override Mage_Adminhtml_IndexController and rewrite the forgotpasswordAction… anybody can help me??
I don´t like the way of magento to recovery a password.

Thanks!!

 
Magento Community Magento Community
Magento Community
Magento Community
 
chris12295
Jr. Member
 
Total Posts:  7
Joined:  2010-02-26
 

Not sure if this applies to CMS Controllers, but for anyone wanting to override controllers that is experincing the “100 router iterations” error, I will try to provide my understanding of the problem and my solution.

The problem is, when you override a controller and set the front name to be the same as the front name of a Mage controller, your module ends up overwriting all controllers in that core module.  In other words, you can’t have the same front name, override a Mage controller, and then not override all the other controllers of that Mage module.

I solved this by creating my own router class.  The standard mage router will use a frontName to reference a module name and if the module has a controller and action execute that action.  In my router, I changed it to a frontName references an array of modules, and foreach module we look for the controller and action.

public function collectRoutes($configArea$routeCode{
        $routes 
Mage::getConfig()->getNode($configArea.'/routers');
        if(
$routes{
            
foreach($routes->children() as $routeName=>$routeConfig{
                
if(!isset($routeConfig->use) || (string)$routeConfig->use != $routeCode) continue;
                
$module = (string)$routeConfig->args->module;
                
$frontName = (string)$routeConfig->args->frontName;
                
$modules $this->getModulesByFrontName($frontName);
                if(!
$modules{
                    $modules 
= array();
                
}
                $modules[] 
$module;
                
$this->addModule($frontName$modules$routeName);
            
}
        }
    }

public function match(Zend_Controller_Request_Http $request{
        
...

        foreach(
$modules as $module)  {
        
...
        
}
}

 
Magento Community Magento Community
Magento Community
Magento Community
Magento Community
Magento Community
    Back to top