[Laravel Company Blog Post]
Creating an AJAX Call to a Custom Controller in Magento
Hello Developers! Today we’re going to tackle a common question I often hear from our clients working on custom Magento projects: how do you create an AJAX call to a custom controller? Let’s dive right into it.
Step 1: Create the Module Structure
The first step is setting up your module correctly. You need two files in the app/etc/modules directory:
BM_Sidebar.xml
<?xml version="1.0"?>
<config>
<modules>
<BM_Sidebar>
<active>true</active>
<codePool>local</codePool>
</BM_Sidebar>
</modules>
</config>
This tells Magento that our module is active and in the local code pool.
Step 2: Create Your Custom Controller
Next, you need to create your actual controller. In this case, we’ll call it IndexController.php:
app/code/local/BM/Sidebar/controllers/IndexController.php
class BM_Sidebar_IndexController extends Mage_Core_Controller_Front_Action {
public function indexAction() {
echo "test data";
}
}
Here, we extend from the core controller and define our action method. For now, it just echoes a simple string.
Step 3: Configuring Your Module
You also need to configure your module using config.xml:
app/code/local/BM/Sidebar/etc/config.xml
<?xml version="1.0"?>
<config>
<modules>
<BM_Sidebar>
<version>0.1.0</version>
</BM_Sidebar>
</modules>
<frontend>
<routers>
<sidebar>
<use>standard</use>
<args>
<module>BM_Sidebar</module>
<frontName>carfilter</frontName>
</args>
</sidebar>
</routers>
<layout>
<updates>
<sidebar>
<file>sidebar.xml</file>
</sidebar>
</updates>
</layout>
</frontend>
</config>
This config tells Magento to use our new sidebar router, and that it should listen for requests under the carfilter URL.
Step 4: The Mystery of Sidebar.xml
Now comes the tricky part - what goes in your sidebar.xml file? This is where you define which controllers get called when a certain URL is requested.
However, in this case, we don’t actually need it. Since our controller is set up correctly (as seen above), Magento will automatically process requests to our carfilter/index route and call the appropriate action method (indexAction).
Step 5: Making the AJAX Call
Finally, your HTML/JavaScript code should make an AJAX request to this URL. Here’s a simple example using jQuery:
$.ajax({
url: 'http://your-magento-site.com/carfilter/index',
success: function(data) {
console.log(data); // Prints "test data"
}
});
Conclusion
And that’s it! You now know how to create an AJAX call from your Magento frontend to a custom controller. The key is correctly setting up the module, controller, and route configuration.
Remember, while this example uses jQuery for simplicity, you can use any modern JavaScript library or framework for making these requests in real-world projects. For more complex scenarios, consider using Magento’s built-in AJAX services or looking into Magento 2’s asynchronous calls.
Happy coding! Let me know if you have any further questions.
[Back to Laravel Company Blog]
*This post was written by Alex, a senior developer at Laravel Company.