1

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

  • Can you add your entire config.xml? Also does the output of "<?php echo Mage::helper('adminhtml')->getUrl('autocustomerlookup/address/checkEmailExists');?>" include the admin route? – Renon Stewart Jul 31 '15 at 20:32
  • Just to make sure we take out the obvious, have you tried to logout of the admin and log back in? Magento reads the ACLs only once, at login, then stores them in the session. When you add a new route to the admin, you have to logout and login again. Also, in the latest security patch, the default ACL in Magento admin controllers is to deny access; if you didn't setup an adminhtml.xml with ACLs, you could implement a public function isAllowed() { return true; } method :D – Adi Aug 2 '15 at 12:28
  • I posted my entire config.xml, with some changes Yes, I've logged in and out, cleared cache and session cookie :) – Matt Aug 4 '15 at 15:56
1

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 Aug 4 '15 at 18:06
  • @Matt Try the code I just added. – Lord Skeletor Aug 7 '15 at 12:19
  • Thanks a lot! I guess I misunderstood something critical about admin controllers and the ACLs. – Matt Aug 7 '15 at 13:50
0

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 Jul 31 '15 at 19:05

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.