Magento: Controller override not working - magento

Attempting to override the Cms/controllers/IndexController.
This is the directory structure of my module -
Local/MyCompany/FourOhFourExp/controllers/IndexController.php
Local/MyCompany/FourOhFourExp/etc/config.xml
The code for my IndexController is
<?php
require_once 'Mage'. DS .'Cms'. DS .'controllers'. DS .'IndexController.php';
class MyCompany_FourOhFourExp_IndexController extends Mage_Cms_IndexController
{
public function noRouteAction($coreRoute = null){
Mage::log('First Function');
header("Location: new_route.html");
die();
}
public function defaultNoRouteAction()
{
Mage::log('Second function!');
header("Location: new_route.html");
die();
}
}
?>
and this is the code for my config.xml file
<?xml version="1.0" encoding="UTF-8"?>
<config>
<modules>
<MyCompany_FourOhFourExp>
<version>0.1.0</version>
</MyCompany_FourOhFourExp>
</modules>
<frontend>
<routers>
<cms>
<args>
<modules>
<MyCompany_FourOhFourExp before="Mage_Cms">MyCompany_FourOhFourExp</MyCompany_FourOhFourExp>
</modules>
</args>
</cms>
</routers>
<events>
<controller_action_noroute>
<observers>
<cms>
<class>cms/observer</class>
<method>noRoute</method>
</cms>
</observers>
</controller_action_noroute>
</frontend>
</config>
Hoping someone can explain why this isn't overriding. I've looked through numerous tutorials and blog posts but have yet to figure out why it doesn't work.

There are lot of issue:
1)Modulepath:
As your controller class is MyCompany_FourOhFourExp_IndexController then path of class should be
app/code/local/MyCompany/FourOhFourExp
Local/MyCompany/controllers/IndexController
Local/MyCompany/etc/config.xml
change to
Local/MyCompany/FourOhFourExp/controllers/IndexController/php
Local/MyCompany/FourOhFourExp/etc/config.xml
2)Mage cms controller donot add properly
require_on Mage::getModuleDir('controllers', 'Mage_Cms'). DS .'IndexController.php';

What need to do:
add to app/code/local/Mage/Cms/controllers/IndexController.php , Mage::dispatchEvent('cms_controller_action_noroute', array('action'=>$this));
in your custom module, catch the Event

Related

How to overwrite magento controller action from own module?

I try to overwrite the function indexAction from "app\code\core\Mage\Newsletter\controllers\ManageController.php" with my own.
I duplicated the controller to app\code\local\Fekete\Newsletter2Go\controllers\ManageController.php and only let the function indexAction there.
<?php
require_once 'Mage/Newsletter/controllers/ManageController.php';
class Fekete_Newsletter2Go_ManageController extends Mage_Newsletter_ManageController
{
public function indexAction()
{
exit("test");
}
}
Then I added the following code inside the <config></config> tags in: app\code\local\Fekete\Newsletter2Go\etc\config.xml:
<frontend>
<routers>
<newsletter>
<args>
<modules>
<Fekete_Newsletter2Go before="Mage_Newsletter">
Fekete_Newsletter2Go
</Fekete_Newsletter2Go>
</modules>
</args>
</newsletter>
</routers>
</frontend>
But If I go to http://example.com/newsletter/manage/ then nothing has changed, my overwrite was not used.
What am I missing?
I found the cause for the problem. I had to change this:
<Fekete_Newsletter2Go before="Mage_Newsletter">
Fekete_Newsletter2Go
</Fekete_Newsletter2Go>
to this:
<Fekete_Newsletter2Go before="Mage_Newsletter">Fekete_Newsletter2Go</Fekete_Newsletter2Go>

Magento paypal express review page and agreements

at the moment i'm trying to commit a little change to Magento's Paypal Express Extension. I'm trying to get rid of the review page because it's unnecessary.
How it's done is alreasy described here:
Magento: easy way to remove "paypal/express/review" step
But it won't work when you enable agreements.
And here's the problem:
app/code/core/Mage/Paypal/Controller/Express/Abstract.php
line 314 to 316 must be uncommented
if (array_diff($requiredAgreements, $postedAgreements)) {
Mage::throwException(Mage::helper('paypal')->__('Please agree to all the terms and conditions before placing the order.'));
}
app/code/community/Sandfox/RemovePaypalExpressReviewStep/etc/config.xml
<?xml version="1.0"?>
<config>
<modules>
<Sandfox_RemovePaypalExpressReviewStep>
<version>0.1.0</version>
</Sandfox_RemovePaypalExpressReviewStep>
</modules>
<global>
<models>
<sandfox_removepaypalexpressreviewstep>
<class>Sandfox_RemovePaypalExpressReviewStep_Model</class>
</sandfox_removepaypalexpressreviewstep>
<paypal>
<rewrite>
<config>Sandfox_RemovePaypalExpressReviewStep_Model_Config</config>
</rewrite>
</paypal>
</models>
<events>
<controller_action_predispatch_paypal_express_review>
<observers>
<sandfox_removepaypalexpressreviewstep>
<type>singleton</type>
<class>sandfox_removepaypalexpressreviewstep/observer</class>
<method>controllerActionPredispatchPaypalExpressReview</method>
</sandfox_removepaypalexpressreviewstep>
</observers>
</controller_action_predispatch_paypal_express_review>
</events>
</global>
<frontend>
<routers>
<paypal>
<args>
<modules>
<Sandfox_RemovePaypalExpressReviewStep before="Mage_Paypal">Sandfox_RemovePaypalExpressReviewStep</Sandfox_RemovePaypalExpressReviewStep>
</modules>
</args>
</paypal>
</routers>
</frontend>
</config>
Now i tried to rewrite the controller (is it really a controller? Why it's not in controllers but has it's own Controller directory?)
app/code/community/Sandfox/RemovePaypalExpressReviewStep/Controller/Express/Abstract.php
<?php
include_once("Mage/Paypal/Controller/Express/Abstract.php");
class Sandfox_RemovePaypalExpressReviewStep_Controller_Express_Abstract extends Mage_Paypal_Controller_Express_Abstract
{
public function placeOrderAction()
{
try {
.
.
.
// if (array_diff($requiredAgreements, $postedAgreements)) {
// Mage::throwException(Mage::helper('paypal')->__('Please agree to all the terms and conditions before placing the order.'));
// }
}
.
.
.
}
At the moment the rewrite doesn't work. Can someone give me a hint?
tec:
Magento 1.9.2
PHP 5.5
MYSQL 5.6.19
In your config.xml, remove the space after module's name
<Sandfox_RemovePaypalExpressReviewStep before="Mage_Paypal">Sandfox_RemovePaypalExpressReviewStep </Sandfox_RemovePaypalExpressReviewStep>
should be changed to
<Sandfox_RemovePaypalExpressReviewStep before="Mage_Paypal">Sandfox_RemovePaypalExpressReviewStep</Sandfox_RemovePaypalExpressReviewStep>
You can't extends an Abstract class. U need to copy into local/Mage...

Magento override controller

I would like to do the above.
Ive overridden many files in the past...block, model, helper....but this one eludes me.
Can anyone see what im doing wrong here:
(ive edited this code...to include some of the recomendations now...)
Heres my folder structure (2 controller locations as a test):
/Idigital/Idgeneral/etc/config.xml
/Idigital/Idgeneral/controllers/Checkout/CartController.php
/Idigital/Idgeneral/controllers/CartController.php
Heres my config.xml:
<?xml version="1.0"?>
<config>
<modules>
<idigital_idgeneral>
<version>0.1.0</version>
</idigital_idgeneral>
</modules>
<global>
<blocks>
<idgeneral><class>Idigital_Idgeneral_Block</class></idgeneral>
</blocks>
</global>
<frontend>
<routers>
<checkout>
<use>standard</use>
<args>
<modules>
<Idigital_Idgeneral before="Mage_Checkout">Idigital_Idgeneral_Checkout</Idigital_Idgeneral>
</modules>
</args>
</checkout>
</routers>
<layout>
<updates>
<idgeneral>
<file>idigital.xml</file>
</idgeneral>
</updates>
</layout>
</frontend>
</config>
Ihave placed my controller file in 2 locations to test.
And heres the top of my FIRST controller file:
require_once 'Mage/Checkout/controllers/CartController.php';
class Idigital_Idgeneral_Checkout_CartController extends Mage_Checkout_CartController
{
public function testAction()
{
var_dump('inside checkout/cart/test');exit;
}
/**
* Add product to shopping cart action
*/
public function addAction()
{
blah...
}
Ans my second controller:
require_once 'Mage/Checkout/controllers/CartController.php';
class Idigital_Idgeneral_CartController extends Mage_Checkout_CartController
{
public function testAction()
{
var_dump('inside cart/test');exit;
}
/**
* Add product to shopping cart action
*/
public function addAction()
{
blah...
}
When i visit: /checkout/cart/add
Im directed to the mage controller...not my local. (i have var_dump stmts in each..so i can see which is ran).
When i visit /checkout/cart/test - i get a 404
When i visit /cart/add or cart/test - i get a 404
when i visit idgeneral/cart/test or idgeneral/cart/add - i get a 404
Create your module folders and files
app/code/local/MyNameSpace/MyModule/etc/config.xml
app/code/local/MyNameSpace/MyModule/controllers/Checkout/CartController.php
app/etc/modules/MyNameSpace_All.xml
Edit /etc/config.xml and Create app/code/local/MyNameSpace/MyModule/etc/config.xml with the following content:
<?xml version="1.0"?>
<config>
<modules>
<MyNameSpace_MyModule>
<version>0.1.0</version>
</MyNameSpace_MyModule>
</modules>
<global>
<!-- This rewrite rule could be added to the database instead -->
<rewrite>
<!-- This is an identifier for your rewrite that should be unique -->
<!-- THIS IS THE CLASSNAME IN YOUR OWN CONTROLLER -->
<mynamespace_mymodule_checkout_cart>
<from><![CDATA[#^/checkout/cart/#]]></from>
<!--
- mymodule matches the router frontname below
- checkout_cart matches the path to your controller
Considering the router below, "/mymodule/checkout_cart/" will be
"translated" to "/MyNameSpace/MyModule/controllers/Checkout/CartController.php" (?)
-->
<to>/mymodule/checkout_cart/</to>
</mynamespace_mymodule_checkout_cart>
</rewrite>
</global>
<!--
If you want to overload an admin controller this tag should be <admin> instead,
or <adminhtml> if youre overloading such stuff (?)
-->
<frontend>
<routers>
<mynamespace_mymodule>
<!-- should be set to "admin" when overloading admin stuff (?) -->
<use>standard</use>
<args>
<module>MyNameSpace_MyModule</module>
<!-- This is used when "catching" the rewrite above -->
<frontName>mymodule</frontName>
</args>
</mynamespace_mymodule>
</routers>
</frontend>
Note: The above didn’t work for me when I override catalog/product controller. I had to use:
<from><![CDATA[#^catalog/product/#]]></from>
<to>mymodule/mycontroller</to>
(notice the missing leading slash)
Since Magento 1.3 you can simply add your module to the frontend router. Rewrites are not neccessary any more:
<?xml version="1.0" encoding="UTF-8"?>
<config>
<modules>
<MyNameSpace_MyModule>
<version>0.1.0</version>
</MyNameSpace_MyModule>
</modules>
<frontend>
<routers>
<checkout>
<args>
<modules>
<MyNameSpace_MyModule before="Mage_Checkout">MyNameSpace_MyModule</MyNameSpace_MyModule>
</modules>
</args>
</checkout>
</routers>
</frontend>
Please note that before=”Mage_Checkout” will load your controller first if available and fall back to Magento’s if not.
If the controller is in another folder, you’ll have to modify the code:
app/code/local/MyNameSpace/MyModule/controllers/Checkout/CartController.php.
Replace
<MyNameSpace_MyModule before="Mage_Checkout">MyNameSpace_MyModule</MyNameSpace_MyModule>
with
<MyNameSpace_MyModule before="Mage_Checkout">MyNameSpace_MyModule_Checkout</MyNameSpace_MyModule>
Edit 'controllers/Checkout/CartController.php'
Create app/code/local/MyNameSpace/MyModule/controllers/Checkout/CartController.php with the following content: (the only change to indexAction() is adding an error_log() call):
<?php
# Controllers are not autoloaded so we will have to do it manually:
require_once 'Mage/Checkout/controllers/CartController.php';
class MyNameSpace_MyModule_Checkout_CartController extends
Mage_Checkout_CartController
{
# Overloaded indexAction
public function indexAction() {
# Just to make sure
error_log('Yes, I did it!');
parent::indexAction();
}
}
Edit 'app/etc/modules/MyNameSpace_All.xml'
(This is to activate your module)
true
local
Edit 'app/design/frontend/[myinterface]/[mytheme]/layout/checkout.xml' and add the following to use the same update handle as before:
<mynamespace_mymodule_checkout_cart_index>
<update handle="checkout_cart_index"/>
</mynamespace_mymodule_checkout_cart_index>
(Note that these tags seem to be case sensitive. Try using all lowercase if this isn’t working for you)
[by Hendy: When I override catalog/product/view using the method described in this Wiki or here, I didn’t have to do the above. However, when using the 'cms way', I had to update the handle manually.]
The above item did not work for me (2009-02-19 by Jonathan M Carvalho)
I discovered that the file to change is app/design/frontend/[myinterface]/[mytheme]/layout/mymodule.xml
Add the following lines:
Point your browser to /checkout/cart/
In your PHP error log, you should find ‘Yes, I did it!’.
You need to be extra precise with the rewrite regular expression because errors are very hard to find.
<Idigital_Idgeneral before="Mage_Checkout">Idgeneral_Checkout</Idigital_Idgeneral>
Should be
<Idigital_Idgeneral before="Mage_Checkout">Idigital_Idgeneral_Checkout</Idigital_Idgeneral>
or try moving your custom controller up to
../Idigital/Idgeneral/controllers/CartController.php
and use
<Idigital_Idgeneral before="Mage_Checkout">Idigital_Idgeneral</Idigital_Idgeneral>
There is also an error in your <modules> tag location. It should be:
<config>
<modules>
<idigital_idgeneral>
<version>0.1.0</version>
</idigital_idgeneral>
</modules>
<global>
...
</global>
<frontend>
....
</frontend>
...
</config>
i.e <modules> shouldn't be inside <global>
Here's a good tutorial on how to dump the config tree that Magento sees as XML: http://alanstorm.com/magento_config
I'm leaving this here for the next poor developer forced to work with this jalopy. Much of the instructions here are pasted from the magento docs which like its source, is a twisted labyrinth of misdirection. Okay enough complaints...
This worked for me in version 1.8
Create your namespace and module:
/app/code/local/MyNameSpace/MyModule
Create your module config:
/app/code/local/MyNameSpace/MyModule/etc/config.xml
<?xml version="1.0" ?>
<config>
<modules>
<MyNameSpace_MyModule>
<version>0.1.0</version>
</MyNameSpace_MyModule>
</modules>
<frontend>
<routers>
<checkout>
<args>
<modules>
<MyNameSpace_MyModule before="Mage_Checkout">MyNameSpace_MyModule_Checkout</MyNameSpace_MyModule>
</modules>
</args>
</checkout>
</routers>
</frontend>
Create your controller:
/app/code/local/MyNameSpace/MyModule/controllers/Checkout/CartController.php
<?php
require_once Mage::getModuleDir('controllers', 'Mage_Checkout').DS.'CartController.php';
class MyNameSpace_MyModule_Checkout_CartController extends Mage_Checkout_CartController
{
public function indexAction() {
// /var/log/debug.log should log the output
Mage::log('cart index override', null, 'debug.log');
// Call the parent class
parent::indexAction();
}
}
Enable your new module:
/app/etc/modules/MyNameSpace_All.xml
<?xml version="1.0" ?>
<config>
<modules>
<MyNameSpace_MyModule>
<active>true</active>
<codePool>local</codePool>
</MyNameSpace_MyModule>
</modules>
</config>
That's all thats needed. Now go celebrate, you just polished a turd! ;)
This is a little notification on the include path of the controller.
This include path can cause errors if the Magento Compiler mode is turned on.
require_once 'Mage/Checkout/controllers/CartController.php';
Instead of that it is good to use
require_once Mage::getModuleDir('controllers', 'Mage_Checkout').DS.'CartController.php';
It will be safer.
Well...it WONT override the checkout cart controller.
So i used the URL REWRITE in the PRODUCT VIEW...from this link...near the bottom of the article. They say its a proper method...
http://www.excellencemagentoblog.com/magento-add-product-to-cart-ajax
if(!url){
url = jQuery('#product_addtocart_form').attr('action');
}
url = url.replace("checkout/cart","idgeneral/cart");
That worked for me. Allows me to get cracking. Basically calls MY controller..instead of checkout controller.
Thanks for the help ROSCIUS...appreciated.
I also had to change my config....my routers section now looks like this:
<routers>
<!-- THIS PART REGISTERS OUR CONTROLLERS FOLDER FOR USE -->
<idgeneral>
<use>standard</use>
<args>
<module>Idigital_Idgeneral</module>
<frontName>idgeneral</frontName>
</args>
</idgeneral>
<!-- THIS PART WONT WORK TO OVERWRITE OUR MAGE CONTROLLER -->
<checkout>
<use>standard</use>
<args>
<modules>
<Idigital_Idgeneral before="Mage_Checkout">Idigital_Idgeneral_Checkout</Idigital_Idgeneral>
</modules>
</args>
</checkout>
</routers>

Magento Controller route is routing to controller's noRouteAction

I'm building a Controller and when I call an Action I get a 404 Error (everything worked last week!). I'm calling the action using:
http://www.website.com/route/controllername/actionname
eg:
http://www.website.com/abcdef/test/echo
I have investigated the problem (using Mage::log('..')) and found out that the controller is being loaded. The controller's name is fine too. Further investigation revealed that the noRouteAction within my controller is being called.
ie my controller contains:
public function noRouteAction() {
echo "noRoute Action";
}
public function echoAction() {
echo "Echo Action";
}
When I call http://www.website.com/abcdef/test/echo I get "noRoute Action".
Why isn't echoAction being called?
Edit:
The config.xml
<?xml version="1.0"?>
<config>
<modules>
<Abcdef_Web>
<version>1.0.0</version>
</Abcdef_Web>
</modules>
<global>
<helpers>
<abcdef>
<class>Abcdef_Web_Helper</class>
</abcdef>
</helpers>
<blocks>
<abcdef>
<class>Abcdef_Web_Block</class>
</abcdef>
</blocks>
</global>
<frontend>
<routers>
<abcdef>
<use>standard</use>
<args>
<module>Abcdef_Web</module>
<frontName>abcdef</frontName>
</args>
</abcdef>
</routers>
<layout>
<updates>
<abcdef>
<file>abcdef.xml</file>
</abcdef>
</updates>
</layout>
</frontend>
</config>
The controller class file: (TestController.php)
<?php
class Abcdef_Web_TestController extends Mage_Core_Controller_Front_Action {
public function noRouteAction() {
echo "noRoute Action";
}
public function echoAction() {
echo "Echo Action";
}
}
?>
OK found the problem!!!!
It seems that "Add Store Code to Urls" was set to yes, so Magento was expecting:
http://www.website.com/storecode/abcdef/test/echo
rather then:
http://www.website.com/abcdef/test/echo
For those with the same problem. This can be disabled through:
System / Configuration / General / Web / Url Options / Add Store Code to Urls
Note:
I do not remember setting this option to Yes. I've seen some reports that this setting could be changing automatically (4 people reporting it):
http://www.magentocommerce.com/boards/viewthread/192613/

Magento Model is not Working

I am following this tutorial
http://www.magentocommerce.com/knowledge-base/entry/magento-for-dev-part-5-magento-models-and-orm-basics.
As per this tutorial,when i pass a value with url like(http://localhost/dev/weblog/index/testModel/id/1), I am getting error like this
Fatal error: Call to a member function load() on a non-object in app\code\local\Srivats\Weblog\controllers\IndexController.php on line 10
This is my index controller code
<?php
class Srivats_Weblog_IndexController extends Mage_Core_Controller_Front_Action
{
public function testModelAction()
{
$params = $this->getRequest()->getParams();
$blogpost = Mage::getModel('weblog/blogpost');
echo("Loading the blogpost with an ID of ".$params['id']);
$blogpost->load($params['id']);
$data = $blogpost->getData();
var_dump($data);
}
}
Blogpost.php file
<?php
class Srivats_Weblog_Model_Mysql4_Blogpost extends Mage_Core_Model_Mysql4_Abstract
{
protected function _construct()
{
$this->_init('weblog/blogpost','blogpost_id');
}
}
Here is my config file
<?xml version="1.0" encoding="utf-8"?>
<config>
<global>
<modules>
<srivats_weblog>
<version>1.0.0</version>
</srivats_weblog>
</modules>
<models>
<weblog_mysql4>
<class>Srivats_Weblog_Model_Mysql4</class>
<resourceModel>weblog_mysql4</resourceModel>
</weblog_mysql4>
</models>
</global>
<frontend>
<routers>
<weblog>
<use>standard</use>
<args>
<module>Srivats_Weblog</module>
<frontName>weblog</frontName>
</args>
</weblog>
</routers>
</frontend>
</config>
system.log shows
Warning: include() [function.include]:
Failed opening 'Mage\Weblog\Model\Blogpost.php' for inclusion
(include_path='app\code\local;E:\dev\app\code\community;app\code\core;E:\dev\lib;.;C:\php\pear')
in lib\Varien\Autoload.php on line 93
Here is the link to all files https://gist.github.com/cf99e4277599954f38d4
I think this line $blogpost->load($params['id']); causing trouble.What i am missing.Any pointers?
Ahh in you config you should have
<global>
<models>
<weblog>
<class>Srivats_Weblog_Model</class>
<resourceModel>weblog_mysql4</resourceModel>
</weblog>
</model>
</global>
you already have for mysql4 resource add this on top of that. You should have
<models>
<weblog>
<class>Srivats_Weblog_Model</class>
<resourceModel>weblog_mysql4</resourceModel>
</weblog>
<weblog_mysql4>
<class>Srivats_Weblog_Model_Mysql4</class>
</weblog_mysql4>
</models>
Your real problem is that the line:
$blogpost = Mage::getModel('weblog/blogpost');
Is not returning an object and so $blogpost is just NULL.
Is the blogpost model class file actually created in app/code/local/Srivats/Weblog/Model/Blogpost.php

Resources