I'm trying to make an AJAX call from the sales order create index in the Magento backend. My url is formed by this function and is returning a 404.
This is the URL it is forming http://127.0.0.1/wsi/index.php/admin/autocustomerlookup/address/checkEmailExists/key/e7dc1ffa2f80b1892da02f80688a29c4/
<?php echo Mage::helper('adminhtml')->getUrl('autocustomerlookup/address/checkEmailExists');?>
I'm basing my work on this Git repo https://github.com/zhukandrey/Atwix_EmailAjax/
I can't figure out why it isn't working, I am certain there is some error with my controller or my routing, trying to make the call from the Magento backend. But I'm stumped!
<config>
<modules>
<TwilitGrotto_AutoCustomerLookup>
<version>1.0.0.0</version>
</TwilitGrotto_AutoCustomerLookup>
</modules>
<global>
<helpers>
<twilitgrotto_autocustomerlookup>
<class>TwilitGrotto_AutoCustomerLookup_Helper</class>
</twilitgrotto_autocustomerlookup>
</helpers>
</global>
<adminhtml>
<layout>
<updates>
<twilitgrotto>
<file>twilitgrotto_autocustomerlookup_layout.xml</file>
</twilitgrotto>
</updates>
</layout>
</adminhtml>
<admin>
<routers>
<adminhtml>
<twilitgrotto_autocustomerlookup>
<use>admin</use>
<args>
<module>TwilitGrotto_AutoCustomerLookup</module>
<modules>
<TwilitGrotto_AutoCustomerLookup after="Mage_Adminhtml">
TwilitGrotto_AutoCustomerLookup
</TwilitGrotto_AutoCustomerLookup>
</modules>
<frontName>autocustomerlookup</frontName>
</args>
</twilitgrotto_autocustomerlookup>
</adminhtml>
</routers>
</admin>
</config>
function handleEmailEvent(){
var email = document.getElementById("email");
var url = document.getElementById('check_email_address_exists').innerHTML;
makeRequest(url, $F(email) );
}
function makeRequest(url, emailAddress){
var httpRequest = new XMLHttpRequest();
console.log(url);
if ( !httpRequest) {
console.log('httpRequest failed');
return false;
}
httpRequest.open( 'POST', url );
httpRequest.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
httpRequest.send('emailAddress=' + encodeURIComponent(email));
console.log(httpRequest);
}
class TwilitGrotto_AutoCustomerLookup_AddressController extends Mage_Adminhtml_Controller_Action{
public function checkEmailExistsAction(){
$emailAddress = $this->getRequest()->getParam('emailAddress');
$result = Mage::helper('twilitgrotto_autocustomerlookup')->checkEmailExists($emailAddress);
$this->getResponse()->setBody($result);
}
}
class TwilitGrotto_AutoCustomerLookup_Helper_Data extends Mage_Core_Helper_Data{
public function checkEmailExists($emailAddress) {
$emailAddressCheck = Mage::getModel('customer/customer')
->getCollection()
->addAttributeToSelect('email')
->addAttributeToFilter('email', $emailAddress)->load();
if(!$emailAddressCheck->getSize()) {
$result = 'ok';
} else {
$result = 'error';
}
return $result;
}
}
Edited to add entire config.xml
2 Answers 2
Looks like your admin route declaration is incorrect. Try this:
<admin>
<routers>
<adminhtml>
<args>
<modules>
<TwilitGrotto_AutoCustomerLookup after="Mage_Adminhtml">TwilitGrotto_AutoCustomerLookup</TwilitGrotto_AutoCustomerLookup>
</modules>
</args>
</adminhtml>
</routers>
</admin>
Here is a good article on the topic: Magento Admin Hello World Revisited
EDIT:
I'm posting a working solution below:
File: TwilitGrotto\AutoCustomerLookup\etc\adminhtml.xml
<?xml version="1.0"?>
<config>
<acl>
<resources>
<admin>
<children>
<sales>
<children>
<twilitgrotto_autocustomerlookup translate="title" module="twilitgrotto_autocustomerlookup">
<title>Auto Customer Lookup</title>
</twilitgrotto_autocustomerlookup>
</children>
</sales>
</children>
</admin>
</resources>
</acl>
</config>
File: TwilitGrotto\AutoCustomerLookup\etc\config.xml
<?xml version="1.0"?>
<config>
<modules>
<TwilitGrotto_AutoCustomerLookup>
<version>1.0.0.0</version>
</TwilitGrotto_AutoCustomerLookup>
</modules>
<global>
<helpers>
<twilitgrotto_autocustomerlookup>
<class>TwilitGrotto_AutoCustomerLookup_Helper</class>
</twilitgrotto_autocustomerlookup>
</helpers>
</global>
<adminhtml>
<layout>
<updates>
<twilitgrotto>
<file>twilitgrotto_autocustomerlookup_layout.xml</file>
</twilitgrotto>
</updates>
</layout>
</adminhtml>
<admin>
<routers>
<adminhtml>
<args>
<modules>
<TwilitGrotto_AutoCustomerLookup after="Mage_Adminhtml">TwilitGrotto_AutoCustomerLookup_Adminhtml</TwilitGrotto_AutoCustomerLookup>
</modules>
</args>
</adminhtml>
</routers>
</admin>
</config>
File: TwilitGrotto\AutoCustomerLookup\controllers\Adminhtml\Autocustomerlookup\AddressController.php
<?php
class TwilitGrotto_AutoCustomerLookup_Adminhtml_Autocustomerlookup_AddressController
extends Mage_Adminhtml_Controller_Action
{
public function checkEmailExistsAction()
{
$emailAddress = $this->getRequest()->getParam('emailAddress');
$result = Mage::helper('twilitgrotto_autocustomerlookup')->checkEmailExists($emailAddress);
$this->getResponse()->setBody($result);
}
protected function _isAllowed()
{
return Mage::getSingleton('admin/session')->isAllowed('sales/twilitgrotto_autocustomerlookup');
}
}
Controller URL: Mage::getUrl('adminhtml/autocustomerlookup_address/checkEmailExists');
-
Thanks, still not working, but that article was very helpful in revising my config.xml, which was definitely a mess.Matt– Matt2015年08月04日 18:06:19 +00:00Commented Aug 4, 2015 at 18:06
-
@Matt Try the code I just added.Lord Skeletor– Lord Skeletor2015年08月07日 12:19:37 +00:00Commented Aug 7, 2015 at 12:19
-
Thanks a lot! I guess I misunderstood something critical about admin controllers and the ACLs.Matt– Matt2015年08月07日 13:50:25 +00:00Commented Aug 7, 2015 at 13:50
One thing I could spot is that your admin controller class should extend:
Mage_Adminhtml_Controller_Action
It is currently extending: Mage_Core_Controller_Front_Action
-
Thanks for that! Still have a 404, but I'm sure that wasn't helping.Matt– Matt2015年07月31日 19:05:17 +00:00Commented Jul 31, 2015 at 19:05
public function isAllowed() { return true; }method :D