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
Overriding customer module: model works, controller doesn’t
 
petemcw
Jr. Member
 
Total Posts:  24
Joined:  2007-08-31
 

I have been trying to create a module that overrides portions of the customer account code.  Eventually, I would like to override both the frontend and admin functionality.  But I am having a hard time getting past what I thought would be basic frontend work.

I have gone throught the following wiki page: How To Override a Controller, but I wasn’t able to get everything working that I need to.

The main piece that I am stuck on is changing the “createPostAction()”.  I have successfully extended the customer entity model, but for some reason I cannot seem to get the controller pieces to work.  I believe it might have something to do with the “<rewrite>” syntax in the “config.xml” file.

File: app/code/local/company/mymodule/etc/config.xml

<?xml version="1.0"?>
<config>
    <
modules>
        <
Company_MyModule>
            <
version>0.1.0</version>
        </
Company_MyModule>
    </
modules>
    <global>
        <
rewrite>
            <
company_mymodule_customer_account>
                <
from><![CDATA[#^/customer/account/$#]]></from>
                
<to>/mymodule/customer_account/</to>
            </
company_mymodule_customer_account>
        </
rewrite>
        <
models>
            <
customer_entity>
                <
rewrite>
                    <
customer>Company_MyModule_Model_Entity_Customer</customer>
                </
rewrite>
            </
customer_entity>
        </
models>
    </global>
</
config>

File: app/code/local/company/mymodule/controllers/Customer/AccountController.php

<?php
/**
 * Customer account controller
 *
 * @category   Mage
 * @package    Mage_Customer
 */
class Company_MyModule_Customer_AccountController extends Mage_Customer_AccountController 
{
    
/**
     * Create customer account action
     */
    
public function createPostAction()
    
{
        
echo 'Hello World!';
        exit;
    
}
}

The module has been enabled in app/etc/modules/Company_All.xml and like I said, I the customer entity module extension works great.  But even a simple override for the action above doesn’t work.  At one point it seemed like things started working once I was logged in as a valid customer, but that obviously doesn’t work if I’m trying to extend the registration (create) process.

I have even tried changing the “config.xml” file to something like this:

File: app/code/local/company/mymodule/etc/config.xml

<?xml version="1.0"?>
<config>
    . . . 
        <
rewrite>
            <
company_mymodule_customer_account>
                <
from><![CDATA[#^/customer/account/createPost$#]]></from>
                
<to>/mymodule/customer_account/createPost</to>
            </
company_mymodule_customer_account>
        </
rewrite>
    . . .
</
config>

Can anybody see what it is I am doing wrong or what I might be missing?  Thanks for the help!

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

in

<from><![CDATA[#^/customer/account/$#]]></from>
regular expression ^/customer/account/$ will match only /customer/account/.

Could you try

<from><![CDATA[#^/customer/account/createPost/?$#]]></from>
<to>/mymodule/customer_account/createPost</to>
 
Magento Community Magento Community
Magento Community
Magento Community
 
petemcw
Jr. Member
 
Total Posts:  24
Joined:  2007-08-31
 

Thanks for the quick reply Moshe.

When I make the change you suggested, it doesn’t seem to do much.  I have verified that the module is active and the model entity changes I have made are taking effect.  I also have the exact “createPostAction()” method defined as above.  When I register as a new customer, all the normal behaviors take place (including my other enhancements).  I would expect that when I hit the Register button that I would see a white page saying “Hello World!”, but instead it successfully creates my account.

A little bit of a piggy back question would be, do I need to have a specific “<rewrite>” block like I have defined or is there a simpler way to extend a controller without having to define every single action that I want extended - the above block only appears to cover the “createPost” action?

 
Magento Community Magento Community
Magento Community
Magento Community
 
petemcw
Jr. Member
 
Total Posts:  24
Joined:  2007-08-31
 

Anyone else have any ideas?  I seem to be able to find a quite a bit on extending models, but I just don’t seem to be having luck with extending the controller.

 
Magento Community Magento Community
Magento Community
Magento Community
 
yshahin
Jr. Member
 
Avatar
Total Posts:  30
Joined:  2008-05-14
 

First I did

<from><![CDATA[#/customer/account/#]]></from>
to match all instances which works
Second dont forget to update the handler in app/design/frontend/default/default/layout/customer.xml
<mynamespace_mymodule_account_create>
        <
update handle="customer_account_create"/>
    </
mynamespace_mymodule_account_create>
Finally and most importantly load the original controller cause it is not autoloaded
by adding require_once ‘Mage/Customer/controllers/AccountController.php’; in the controller class
 
Magento Community Magento Community
Magento Community
Magento Community
 
7th SENSE new media GmbH
Jr. Member
 
Avatar
Total Posts:  25
Joined:  2008-04-03
Reutlingen, Germany
 

Hi,

same problem, the controller wouldn’t be override. i want to change the order status after capturing an invoice. so i need to override the following controller: Mage/Adminhtml/controllers/Sales/Order/InvoiceController.php. i copied this file to my module.

File SeventhSense/Sparkasse/controllers/Adminhtml/Sales/Order/InvoiceController.php:

<?php

class SeventhSense_Sparkasse_Adminhtml_Sales_Order_InvoiceController extends Mage_Adminhtml_Sales_Order_InvoiceController
{
    
/**
     * Capture invoice action
     */
    
public function captureAction()
    
{
        
if ($invoice $this->_initInvoice()) {
            
try {
                $invoice
->capture();
                
$this->_saveInvoice($invoice);
                
$this->_getSession()->addSuccess($this->__('Invoice was successfully captured and new status was set'));

                
### BEGIN EDIT - Change the order status ###
                
$order $invoice->getOrder();
                
$order->setStatus(Mage_Sales_Model_Order::STATE_COMPLETE);
                
$order->setState(Mage_Sales_Model_Order::STATE_COMPLETEfalse);
                
$order->addStatusToHistory($order->getStatus(), 'Payment complete'false);
                
$order->save();
                
### END EDIT - Change the order status ###
            
}
            
catch (Mage_Core_Exception $e{
                $this
->_getSession()->addError($e->getMessage());
            
}
            
catch (Exception $e{
                $this
->_getSession()->addError($this->__('Invoice capture error'));
            
}
            $this
->_redirect('*/*/view', array('invoice_id'=>$invoice->getId()));
        
}
        
else {
            $this
->_forward('noRoute');
        
}
    }
}

File config.xml:

<?xml version="1.0"?>

<config>
   <
modules>
       <
SeventhSense_Sparkasse>
           <
version>0.1.0</version>
        </
SeventhSense_Sparkasse>
    </
modules>

    <global>
    <
models>
        <
sparkasse>
            <class>
SeventhSense_Sparkasse_Model</class>
        </
sparkasse>
        <
rewrite>
            <
sparkasse_sales_order_invoice_capture>
                <
from><![CDATA[#^/admin/sales_order_invoice/capture/?$#]]></from>
                
<to>/sparkasseadmin/adminhtml_sales_order_invoice/capture/</to>
            </
sparkasse_sales_order_invoice_capture>
        </
rewrite>
    </global>

    <
adminhtml>
        <
routers>
            <
sparkasse>
                <use>
admin</use>
                <
args>
                    <
module>SeventhSense_Sparkasse</module>
                    <
frontName>sparkasseadmin</frontName>
                </
args>
            </
sparkasse>
        </
routers>
    </
adminhtml>

File app/etc/modules/SeventhSense_Sparkasse.xml

<?xml version="1.0"?>
<config>
    <
modules>
        <
SeventhSense_Sparkasse>
            <
active>true</active>
            <
codePool>local</codePool>
            <
depends>
                <
Mage_Payment />
                <
Mage_Paypal />
                <
Mage_BankPayment />
            </
depends>
        </
SeventhSense_Sparkasse>
    </
modules>
</
config>

To override a controller in the Backend, i don’t need to edit/update the design/adminhtml/default/default/layout/main.xml ?

 
Magento Community Magento Community
Magento Community
Magento Community
 
mzentrale1
Guru
 
Total Posts:  731
Joined:  2007-12-06
Stuttgart, Germany
 

Hi,

i also had problems with overwriting an controller by using the config.xml file. The rewrite never works. So i changed the action of the button or the link in block or phtml in my own design. But schouldnt the rewrite part look like this:

<to>/sparkasse/adminhtml_sales_order_invoice/capture/</to>

instead of

<to>/sparkasseadmin/adminhtml_sales_order_invoice/capture/</to>

cheers

Stefan

 
Magento Community Magento Community
Magento Community
Magento Community
 
7th SENSE new media GmbH
Jr. Member
 
Avatar
Total Posts:  25
Joined:  2008-04-03
Reutlingen, Germany
 

The rewrite never works. So i changed the action of the button or the link in block or phtml in my own design.

Hi Stefan,

thanks for that hint. now it works grin

my new config.xml:

<?xml version="1.0"?>

<config>
   <
modules>
       <
SeventhSense_Sparkasse>
           <
version>0.1.0</version>
        </
SeventhSense_Sparkasse>
    </
modules>

    <global>
        <
models>
            <
sparkasse>
                <class>
SeventhSense_Sparkasse_Model</class>
            </
sparkasse>
        </
models>
        <
blocks>
            <
adminhtml>
                <
rewrite>
                  <
sales_order_invoice_view>SeventhSense_Sparkasse_Block_Adminhtml_Sales_Order_Invoice_View</sales_order_invoice_view>
                </
rewrite>
            </
adminhtml>
        </
blocks>
    </global>

    <
adminhtml>
        <
routers>
            <
sparkasse>
                <use>
admin</use>
                <
args>
                    <
module>SeventhSense_Sparkasse</module>
                    <
frontName>sparkasse</frontName>
                </
args>
            </
sparkasse>
        </
routers>
    </
adminhtml>
<
config>

File SeventhSense/Sparkasse/cBlock/Adminhtml/Sales/Order/Invoice/View.php

class SeventhSense_Sparkasse_Block_Adminhtml_Sales_Order_Invoice_View extends Mage_Adminhtml_Block_Sales_Order_Invoice_View
{
    
public function getCaptureUrl()
    
{
        
return $this->getUrl('sparkasse/adminhtml_sales_order_invoice/capture', array('invoice_id'=>$this->getInvoice()->getId()));
    
}
}

cheerio,
Markus

 
Magento Community Magento Community
Magento Community
Magento Community
 
Zardinuk
Jr. Member
 
Total Posts:  7
Joined:  2008-07-14
 

I was never able to get this controller override to work either, have tried everything.... could someone please help?

I am using the latest SVN copy, I’ve got mod_rewrite enabled, my namespace is Addtech and my module name is Checkout , could the name Checkout be causing a conflict?  I just get a 404 error consistently… I can’t do anything. Also, I tried turning on the logging opions in the admin section and nothing, I can’t get any logs either (have made sure the permissions were allowing it, even made the var/log folder).

Here is my config.xml

<?xml version="1.0"?>
<config>
    <
modules>
        <
Addtech_Checkout>
            <
version>0.1.0</version>
        </
Addtech_Checkout>
    </
modules>
    <global>
        <
rewrite>
            <
addtech_checkout_onepage_install>
                <
from><![CDATA[#^/checkout/onepage/#]]></from>
                
<to>/addtech/checkout_onepage</to>
            </
addtech_checkout_onepage_install>
        </
rewrite>
        <
blocks>
            <
checkout>
                <
rewrite>
                    <
onepage>Addtech_Checkout_Block_Onepage</onepage>
                    <
onepage_installation>Addtech_Checkout_Block_Onepage_Installation</onepage_installation>
                </
rewrite>
                <class>
Addtech_Checkout_Block</class>
            </
checkout>
        </
blocks>
    </global>
    <
frontend>
        <
routers>
            <
addtech_checkout>
                <use>
standard</use>
                <
args>
                    <
module>Addtech_Checkout</module>
                    <
frontName>addtech</frontName>
                </
args>
            </
addtech_checkout>
        </
routers>
    </
frontend>
</
config>

I’d really like to get this to work, my only other option is to start modifying the core onepage checkout code. Does anybody see a flaw with this code? Any suggestions how to troubleshoot it?

 
Magento Community Magento Community
Magento Community
Magento Community
 
Zardinuk
Jr. Member
 
Total Posts:  7
Joined:  2008-07-14
 

a little more information, nothing at all will register with the addtech prefix, it seems the routing doesn’t work at all, the rewrite appears to be working though.

 
Magento Community Magento Community
Magento Community
Magento Community
 
bnwla
Jr. Member
 
Total Posts:  6
Joined:  2008-07-23
 

Hi all,

I’m having the same problem with overriding updatePostAction() method in CartController. I did follow the Wiki page direction and tried almost every combination of different XML tags in config.xml and directory structures. It never works. Strangely, it successfully overrides indexAction() method in CartController.

Has anyone found a solution to this problem? Thanks in advance.

 
Magento Community Magento Community
Magento Community
Magento Community
 
bnwla
Jr. Member
 
Total Posts:  6
Joined:  2008-07-23
 

Hi all,

After many many hours, I finally succeeded in overriding a controller (updatePostAction() in CartController). Here’s what I did:

First, I followed the Wiki page in Knowledge Base. Then I made the following changes to config.xml and /layout/mymodule.xml:

In config.xml

<mage_mymodule_checkout_cart_updatePost>
                <
from><![CDATA[#^/checkout/cart/updatePost/$#]]></from>
                
<to>/mymodule/checkout_cart/updatePost</to>
            </
mage_mymodule_checkout_cart_updatePost>

In /layout/mymodule.xml:

<mymodule_checkout_cart_updatePost>
                <
update handle="checkout_cart_updatePost"/>            
        </
mymodule_checkout_cart_updatePost>

Hope it helps.

 
Magento Community Magento Community
Magento Community
Magento Community
 
mzentrale1
Guru
 
Total Posts:  731
Joined:  2007-12-06
Stuttgart, Germany
 

Hi,

thanks for that hint, now it also works in my module!

Big up!

Cheers

Stefan

 
Magento Community Magento Community
Magento Community
Magento Community
 
Georges@Bitbol
Sr. Member
 
Avatar
Total Posts:  104
Joined:  2008-08-07
Paris, France
 

Could anyone explain me the interest of the path MyNameSpace/MyModule/controllers/Checkout/CartController.php
In my opinion, it should be MyNameSpace/Checkout/controllers/CartController.php.
I do not understand what mymodule should be. In code directories, it is checkout.

Is it only for routing and rewrite ?
I ask many times but nobody never explain it.

I succeed in rewrite like the example. I did only this in my controller

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

/**
 * Shopping cart controller
 */
class Lsim_Frontend_Checkout_CartController extends Mage_Checkout_CartController{


    
public function indexAction() {
    
        error_log
('test');
        
parent::indexAction();
    
}
}

Strangely, my page is not the cart one. It is the “default one”. (see attachment)

Thaht’s the second time this stuff is display and I never succeed.

Image Attachments
Image 2.png
 
Magento Community Magento Community
Magento Community
Magento Community
 
maijar
Member
 
Total Posts:  49
Joined:  2008-06-15
 
bnwla - 29 July 2008 02:57 PM

Hi all,

After many many hours, I finally succeeded in overriding a controller (updatePostAction() in CartController). Here’s what I did:

First, I followed the Wiki page in Knowledge Base. Then I made the following changes to config.xml and /layout/mymodule.xml:

My code wasn’t that successful. Can you show what those codes were before you made changes?

I’m trying to override createPostAction() in Customer/controllers/AccountController.php. I’m wondering do I have to make any changes to layout file, because createPost doesn’t contain anything that must be visible, just redirects. (I guess no grin)

 
Magento Community Magento Community
Magento Community
Magento Community
 
alkarim
Guru
 
Total Posts:  368
Joined:  2008-04-10
 

hi there,

I am working at sales order invoice grid now.... there are some changes in there but I don’t want to change core than I make it at local. When I created just like you guys did, I got an error message : Fatal error: Call to a member function toHtml() on a non-object in /home/alkarim/vm/app/code/core/Mage/Adminhtml/Block/Catalog/Product/Attribute/Edit/Tabs.php on line 50,

Can anybody see what it is I am doing wrong or what I might be missing?  Thanks

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