Magento Forum

   
How to create a new controller? 
 
Kristianto
Jr. Member
 
Total Posts:  2
Joined:  2008-11-10
 

Hi everybody!

I need to create a new controller on app/code/local/MyCompany/Catalog/controllers
I have tried out this acrticle:
http://www.magentocommerce.com/boards/viewthread/15914/

I can override an existing method on existing controller,
but failed to add new method on my controller.

The case:

1. Created app\code\local\MyCompany\Catalog\controllers\ProductController.php

class MyCompany_Catalog_ProductController extends Mage_Catalog_ProductController
{
    
public function viewAction()
    
{
        
exit('viewAction executed');
    
}
    
    
public function fabricAction()
    
{
        
exit('fabricAction executed');
    
}
}

2. Created app\code\local\MyCompany\Catalog\etc\config.xml

<modules>
        <
MyCompany_Catalog>
            <
version>0.0.1</version>
            <
depends></depends>
        </
MyCompany_Catalog>
    </
modules>
    
    <global>
        
        <
routers>
            <
catalog>
                <
rewrite>
                    <
product>
                        <
to>MyCompany_Catalog/product</to>
                        <
override_actions>true</override_actions>
                        <
actions>
                            <
view><to>MyCompany_Catalog/product/view</to></view>
                            <
fabric>MyCompany_Catalog/product/fabric</fabric>
                        </
actions>
                    </
product>
                </
rewrite>
            </
catalog>
        </
routers>
    <global>

    <
frontend>
        
        <
routers>
            <
mycompany_catalog_routes>
                <use>
standard</use>
                <
args>
                    <
module>MyCompany_Catalog</module>
                    <
frontName>mycompany_catalog_routes</frontName>
                </
args>
            </
mycompany_catalog_routes>
        </
routers>
        
        <
events></events>
        <
translate></translate>
        <
layout></layout>
    </
frontend>
</
modules>
]

As result of my code, i can override view method on ProductController
and see “viewAction executed” on http://myhost/index.php/catalog/product/view

But new method “fabric” do not work and when access to http://myhost/index.php/catalog/product/fabric,
i got “404 Not Found” error.

My question is:
1. how to create new method on MyCompany\Catalog\controllers\ProductController.php ?
2. how to create new controller, i.e MyCompany\Catalog\controllers\MyProductController.php ?

Anybody can help me?

Thanks a lot!

 
Magento Community Magento Community
Magento Community
Magento Community
 
Kristianto
Jr. Member
 
Total Posts:  2
Joined:  2008-11-10
 

Finally i can solve the problem
I can override and modify the controller

by adding

<admin>
        <
routers>
            <
catalog>
                <use>
admin</use>
                <
args>
                    <
module>MyCompany_Catalog</module>
                    <
frontName>catalog</frontName>
                </
args>
            </
catalog>
        </
routers>
    </
admin>

on MyCompany/Catalog/etc/config.xml

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

Hi, your thread helped me a lot, but the last one is not working in latest magento 1.2.1, generating redirection.
I have checked the reference links also http://www.magentocommerce.com/boards/viewthread/15914/. and found the main problem

It is required to put unique frontName

below the modified approach

<!---- do not use ---->
<admin>
<routers>
<catalog>
<use>admin</use>
<args>
<module>MyCompany_Catalog</module>
<frontName>catalog</frontName>
</args>
</catalog>
</routers>
</admin>
<!---- do not use ---->

<!---- use ---->
<frontend>
<routers>
<cataloge>
<use>standard</use>
<args>
<module>MyCompany_Catalog</module>
<frontName>cataloge</frontName>
</args>
</cataloge>
</routers>
</frontend>
<!---- use ---->

with the new approach you will be able to load it as

magento/cataloge/view/id/?  note the diffrence in front name “cataloge” not “catalog”

Voila!!!!!!!!!!!!

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