Solving Magento AJAX Controller Call Issues
You’re facing difficulties calling a controller from an admin panel template (phtml) file using AJAX. Let’s troubleshoot this issue step by step.
Understanding the Current Setup
Routing Configuration
Your config.xml file correctly defines a new router for the SampleFolder module:
<routers>
<Samplefolder>
<use>admin</use>
<args>
<module>MyModule_Samplefolder</module>
<frontName>SampleFolder</frontName>
</args>
</Samplefolder>
</routers>
This configuration tells Magento to use the admin router for your SampleFolder module and sets its front name.
Block File
In your Block/Adminhtml/OnePageFolder.php, you’ve defined a method to get the URL for adding a card:
public function getAddCardUrl()
{
$url = Mage::helper('adminhtml')->getUrl('SampleFolder/OnePageFolder/AddCard');
return $url;
}
This helper method generates a URL that points to your AddCard action in the OnePageFolderController.
Template File
In your template file (app/design/adminhtml/default/default/template/Samplefolder/OnePageFolder.phtml), you’ve retrieved the URL successfully:
var url = '<?php echo $this->getAddCardUrl(); ?>';
This URL looks correct and points to http://mywebsite.com/index.php/admin/SampleFolder/OnePageFolder/AddCard/key/17cf35ef0cae06459e7acf46b7e2e6d5/.
The AJAX Issue
You’ve written an ajaxCall function to send a POST request to this URL with JSON data:
function ajaxCall(url, customer_id, club_id)
{
jQuery.ajax({
url: url,
type: 'POST',
data: {customer_id:customer_id, club_id:club_id},
dataType: 'json',
success: function(json) {
if (json.response == 'ok') {
alert('good');
} else {
alert('Erreur : '+ json.message);
}
}
});
}
However, the controller action is not being reached. You mentioned you’re seeing the error “router match 100 iteration” in your AJAX callback.
The Controller Problem
Your OnePageFolderController.php file has a AddCardAction() method that’s not getting hit:
/**
* Add club card
*
*/
public function AddCardAction()
{
var_dump('this code is not reached');
die;
}
Even when you call this action directly from the URL, it doesn’t work.
The Solution: Check Your Controller Namespace
The issue likely lies in your controller’s namespace or routing configuration. Here are a few things to check:
-
Controller Namespace: Ensure that the
OnePageFolderControlleris correctly placed within your module structure. It should be located atapp/code/local/MyModule/Samplefolder/controllers/OnePageFolderController.php. -
Controller Naming Convention: The controller class name must match the filename exactly, including capitalization. So, if your file is named
OnePageFolderController.php, your class name should beMage_Samplefolder_OnepagefolderController. -
Method Name Case Sensitivity: Magento uses camelCase for method names in controllers. Ensure your action methods (like
AddCardAction) are written in this format. -
Module Configuration: Double-check your module’s
config.xmland confirm the<frontName>matches what you’ve used in your controller file and URL generation.
Conclusion
Remember, Magento follows a specific naming convention for controllers, actions, and modules. Ensure all your files and methods are named correctly, following the standard camelCase format.
If you’re still having issues, it might be helpful to review the official Magento documentation on Admin Controller Routing for more details.
Don’t hesitate to reach out to the Magento community or consult with a professional developer if you need further assistance. Happy coding!