2

I have a file as follows:

app/code/local/Mycompany/Catalog/controllers/ProductController.php

and a uniquely defined action in that file:

public function sendfriendemailAction() ..

However the URL to post to that action is as follows:

<form action=<?php echo $this->getUrl('ajax/product/sendfriendemail');?>" .. >

The string in the form action doesn't even remotely compare to the path where the actual action method is found (and BTW it does work).

How does Magento figure out that that URL maps to that location? Thank you for insight on how Magento thinks.

5

'ajax/product/sendfriendemail' is not a core URL,
Most probably you have a module that contains this in the config.xml

<frontend>
    <routers>
       <ajax>
            <use>standard</use>
            <args>
                <module>[Namespace]_[Module]</module>
                <frontName>ajax</frontName>
            </args>
        </ajax>
    </routers>
</frontend>

Notice the <frontName>ajax</frontName>. That's the first part of the URL ajax/product/sendfriendemail.
Magento looks in the merged config for a node with frontName identical to the first part of the url and when it finds it it maps the rest of the url relative to that module.
So in the example above ajax corresponds to the module [Namespace]_[Module] so magento will try to match the rest of the url product/sendfriendemail to the controller ProductController from the module [Namespace]_[Module] and execute the method sendfriendemailAction.

This is just the main part.
There are exceptions. you can tell magento for the same key (ajax in this case) to look in multiple modules in a specific order and call the first valid method it finds in a valid controller.

For this, you have to define the routes for your module like this:

    <routers>
        <standard>
            <args>
                <modules>
                    <[Namespace]_[Module] before="[Other]_[Module]">[Namespace]_[Module]</[Namespace]_[Module]>
                </modules>
            </args>
        </standard>
    </routers>

This means that the module [Namespace]_[Module] will use the same frontName as the module [Other]_[Module]. If the rest of the url cannot be mapped to a controller and an action from [Namespace]_[Module] it will try to map it to a controller and action from the initial module [Other]_[Module]

  • By the way, you mention, not a core URL, how are you determining that and is there some resource listing core routing URLs? – Oliver Williams Jan 7 '17 at 14:37
2

I'll answer this with an example.

In app/code/core/Mage/Catalog/etc/config.xml you can see the controllers defined in the <routers> tag like so:

<frontend>
    <routers>
        <catalog>
            <use>standard</use>
            <args>
                <module>Mage_Catalog</module>
                <frontName>catalog</frontName>
            </args>
        </catalog>
    </routers>

This configuration is used to tell magento that when you go to /catalog that it should look for the controllers in the Mage_Catalog module.

catalog/product would then try and resolve Mage_Catalog_ProductController by doing something like.

Mage::getConfig()->getModuleDir('controllers', 'Mage_Catalog') . '/ProductController.php';

and doing something like catalog/product/view would look for a public function viewAction within the returned controller.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.