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

Overloading a controller
 
johnnybravoh
Jr. Member
 
Total Posts:  10
Joined:  2007-11-21
 

Hello,

I am trying to figure out how to integrate some functionality into the catalog_product admin tool to make certain editing easier.  I have come to the conclusion that I need to overload the controller Adminhtml/controllers/Catalog/ProductController.php, but I can’t figure out how to do it.  I have read the post http://www.magentocommerce.com/boards/viewthread/1286/#t6327 but I’m not clear what config.xml file to edit and where to place my ProductControllers.php file.  If it’s possible I would greatly appreciate an example of what files to edit and where to achieve this functionality.

Thanks in advance,

Jon

 
Magento Community Magento Community
Magento Community
Magento Community
 
Jonathan Hedrén
Member
 
Total Posts:  67
Joined:  2007-08-31
Sweden
 

I am also interested in how to extend a controller class. I’ve tried to do that in the same way you do with modules and blocks but it doesn’t seem to work.

 
Magento Community Magento Community
Magento Community
Magento Community
 
Jonathan Hedrén
Member
 
Total Posts:  67
Joined:  2007-08-31
Sweden
 

Now I’ve spent quite a lot of time trying to overload a couple of controllers without success. I’m started to get frustrated rasberry

The reason to why I need to overload a controller is that I’ve been adding custom steps in the checkout flow. My module works well when I edit the core controller files directly but I haven’t been able to extend those classes.

I’ve tried to make a rewrite rule, which I read about in http://www.magentocommerce.com/boards/viewthread/1286/, but I’m not sure that I’m doing it right (I’m not even completely sure that’s the way to extend a controller). This is how I do it:

<global>
           <
rewrite>
               <
MyRewrite>
                   <
from><![CDATA[#^/checkout/onepage/saveCustomData$#]]></from>
                   
<to>mymodule/checkout/onepage</to>
               </
MyRewrite>
           </
rewrite>
</global>

I’m not sure what to set the <to> value to. I’ve tried setting it to a lot of things without success. I want it to point to an extended class called “MyCompany_MyModule_Checkout_OnepageController” which is placed in app/code/local/MyCompany/MyModule/Checkout/controllers/OnepageController.php. In this class I then have a “saveCustomDataAction"-function. How could this be done?

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

Hi Jonathan,

As Moshe said in this topic, it looks like you’re using the method for older releases of Magento. Which release are you working with ?
Try with the other solution:

insert into core_url_rewrite (store_idid_pathrequest_pathtarget_path
values (0'my_rewrite/1''somemodule/controller''anothermodule/controller');
 
Magento Community Magento Community
Magento Community
Magento Community
 
Jonathan Hedrén
Member
 
Total Posts:  67
Joined:  2007-08-31
Sweden
 
SeL - 11 December 2007 01:48 AM

Hi Jonathan,

As Moshe said in this topic, it looks like you’re using the method for older releases of Magento. Which release are you working with ?
Try with the other solution:

insert into core_url_rewrite (store_idid_pathrequest_pathtarget_path
values (0'my_rewrite/1''somemodule/controller''anothermodule/controller');

I’m using 0.6.14100.

I’ve tried adding the rewrite into the database without success. I think the problem is that I don’t really know how the target_path should look like.

My request_path is something like “checkout/onepage/saveSomeData”
My class is “MyCompany_MyModule_Checkout_OnepageController” and the path to it is app/code/local/MyCompany/MyModule/Checkout/controllers/OnepageController.php.

I’ve tried different solutions with the target_path:
“mycompany/mymodule/checkout/onepage”
“mycompany_mymodule_checkout/onepage”
“mycompany_mymodule/checkout_onepage”
“mycompany_mymodule/checkout/onepage”
“mycompany_mymodule_checkout_onepagecontroller”
etc.

None of them are working.

Also, what is “id_path” used for?

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

request_path is what you want to overload:
“checkout/onepage” (right ?)

target_path is what you want to be called (your module’s controller):
“mymodule/checkout/onepage”

Your class must override Mage_Checkout_OnepageController and you need a saveSomeDataAction() method.

First check that it works when you call directly “checkout/onepage/saveSomeData”.
Then, check that when you call “checkout/onepage”, you have the same result.

id_path is used for having a unique id for your path. You can use wathever you want but it must be unique.

Let us know if it’s working.

If not, could you try changing your module class to “Mage_MyModule_Checkout_OnepageController” instead of “MyCompany_...” ?
I don’t know if community’s modules must have their name beginning with Mage_. May be someone at Varien can tell us ?

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

@Jonathan: did you declare a route for your controllers:

<config>
...
  <
frontend>
    <
routers>
      <
mymodule>
        <use>
standard</use>
        <
args>
          <
module>Mage_MyModule</module>
          <
frontName>mymodule</frontName>
        </
args>
      </
mymodule>
    </
routers>
  </
frontend>
 
Magento Community Magento Community
Magento Community
Magento Community
 
Jonathan Hedrén
Member
 
Total Posts:  67
Joined:  2007-08-31
Sweden
 
Moshe - 11 December 2007 08:29 AM

@Jonathan: did you declare a route for your controllers:

Now I’ve done that smile I also moved my controllers from MyCompany/MyModule/OverriddenModule/controllers/ to MyCompany/MyModule/controllers/ which worked much better.

Still I don’t completely understand how controllers/routers/rewrites works and I got another problem now…

My routing config looks like this:

<config>
...
  <
frontend>
    <
routers>
      <
mymodule>
        <use>
standard</use>
        <
args>
          <
module>MyCompany_MyModule</module>
          <
frontName>checkout</frontName>
        </
args>
      </
mymodule>
    </
routers>
  </
frontend>

This config makes all my code run flawlessly, but also affects /checkout/cart/ which returns a 404 and that’s not so good :S

So how am I supposed to do when I want to run my own ‘saveSomeDataAction’ and override ‘saveBillingAction’ in my own controller (which in this case extends OnepageController)? I’d rather not change the urls which is used in the checkout process.

Btw, thanks a lot for your support!

 
Magento Community Magento Community
Magento Community
Magento Community
 
Jonathan Hedrén
Member
 
Total Posts:  67
Joined:  2007-08-31
Sweden
 

I’ve also tried this:

<config>
    ...
    <global>
        ...
        <
rewrite>        
            <
mymodule_onepage>
                <
from><![CDATA[#^/checkout/onepage/$#]]></from>
                
<to>/mymodule/onepage/</to>
            </
mymodule_onepage>
        </
rewrite>
    </global>
    <
frontend>
        <
routers>
            <
mymodule>
                <use>
standard</use>
                <
args>
                    <
module>MyCompany_MyModule</module>
                    <
frontName>mymodule</frontName>
                </
args>
            </
mymodule>
        </
routers>
     </
frontend>
</
config>

The rewrite works fine and my code is running, but when I point my browser to /checkout/onepage/ it loads a 3-col layout with an empty content area.

It feels like I’m missing something crucial…

 
Magento Community Magento Community
Magento Community
Magento Community
 
Michae1
Enthusiast
 
Total Posts:  826
Joined:  2007-08-31
 

Hi Jonathan,

I guess the prblem is that it uses the default layout with no proper layout updates as the layout update handle is determined by your current route and not the original one.

Could you please try to find in the app/design/default/default/layout/main.xml the section

<checkout_onepage_index>
   ...
</
checkout_onepage_index>

and copypaste the entire content of it to the new section you have to create:

<mymodule_onepage_index>
   ...
</
mymodule_onepage_index>

Please let me know if it helps.

Thank you,
Michael.

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

You could also do something like that, if your layout doesn’t differ from original:

<mymodule_onepage_index>
   <
update handle="checkout_onepage_index"/>
</
mymodule_onepage_index>
 
Magento Community Magento Community
Magento Community
Magento Community
 
Jonathan Hedrén
Member
 
Total Posts:  67
Joined:  2007-08-31
Sweden
 

Yep, I got it to work with a layout update handle. Thanks!

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