0

I added a button to admin order view which sends request to an url of an action controller (using ajax), and now I need to somehow make it available only for admin. What is the best solution for this problem? Also I can abandon ajax if there is a better option for the button.

Possible solution that occurred to me is to add something to header and then check it in the action function.

1 Answer 1

0

Your AJAX controller should use admin router and that's it.

app/code/YourVendor/YourModule/etc/adminhtml/routes.xml:

<?xml version="1.0" ?>
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:App/etc/routes.xsd">
    <router id="admin">
        <route id="modulename" frontName="modulename">
            <module name="VendorName_ModuleName" before="Magento_Backend"/>
        </route>
    </router>
</config>

Your AJAX controller should be placed under Controller/Adminhtml folder, eg. app/code/YourVendor/YourModule/Controller/Adminhtml/Index/Index and it should extend \Magento\Backend\App\Action.

Then, use $block->getUrl('modulename/index/index') in your admin block and use generated URL for your AJAX.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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