I want to make a post request to a backend custom controller, which does not work

My controller resides in


<?php namespace Namespace\ModuleName\Controller\Adminhtml\Discount;
class Index extends \Magento\Backend\App\Action

    public function execute()
        $discountAmount = $this->getRequest()->getParam('discountAmount');



My routes.xml in Namespace/ModuleName/etc/adminhtml/routes.xml looks like this

<?xml version="1.0"?>
 * Copyright © 2016 Magento. All rights reserved.
 * See COPYING.txt for license details.
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:App/etc/routes.xsd">
    <router id="admin">
        <route id="adminhtml" frontName="adminhtml">
            <module name="Namespace_ModuleName" />

In my block I'm calling the url by <?php echo $this->getUrl('adminhtml/discount/index'); ?>

This does not seem to create the correct url anyway,because when I try to use this url, it simply redirects to the dashboard

  • Have your file inside index folder, Namespace/ModuleName/Controller/Adminhtml/Discount/Index/Index.php or inside Discount folder? Mar 24 '17 at 6:54
  • sorry for confusion, its Namespace/ModuleName/Controller/Adminhtml/Discount/Index.php
    – xhallix
    Mar 24 '17 at 8:02

When in the adminhtml scope, there is no need to use 'adminhtml/' in the getUrl() method.

The correct to fetch the URL should be: <?php echo $this->getUrl('discount/index'); ?>

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.