0

I'm having troubles in defining a new custom url in admin area.

Here is my routes.xml file, saved in /app/code/Vendor/Module/

<?xml version="1.0" encoding="UTF-8"?>
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:App/etc/routes.xsd">
    <router id="admin">
        <route id="unlock">
            <module name="Vendor_Module" />
        </route>
    </router>
</config>

Then I've created a custom button in the cart rule edit page

namespace Vendor\Module\Block\Adminhtml\SalesRule\Edit\Button;
use Magento\Catalog\Block\Adminhtml\Product\Edit\Button\Generic;
class Unlock extends Generic
{
    protected $request;
    protected $urlBuilder;

    public function __construct(
        \Magento\Framework\App\Request\Http $request,
        \Magento\Framework\UrlInterface $urlBuilder
    ) {
       $this->request = $request;
       $this->urlBuilder = $urlBuilder;
       }


    public function getUnlockUrl($id)
    {
        return $this->urlBuilder->getUrl('*/*/unlock', ['id' => $id]);
    }

    public function getButtonData()
    {
        $id = (int)$this->request->getParam('id');
        if ($id) {
            return [
                'label' => __('Unlock'),
                'on_click' =>  'deleteConfirm(' . json_encode(__('Are you sure you want to Unlock?'))
                    . ','
                    . json_encode($this->getUnlockUrl($id))
                    . ')',
                'sort_order' => 100
            ];
        }
    }
}

Button works, but when I click it Magento redirects to a 404 page. What is the correct url, giving the above code? Do I making mistakes in using the getUrl() method?

  • what is a controller path for this delete URL? – Nits Oct 16 at 17:12
  • app/code/Vendor/Module/Controller/Adminhtml/Index/Unlock.php – Glen Runciter Oct 16 at 17:21
  • Can you please try this url and see it is working or not return $this->urlBuilder->getUrl('unlock/index/unlock', ['id' => $id]); – Nits Oct 16 at 17:44
2

routes.xml file in /app/code/Vendor/Module/etc/adminhtml

<?xml version="1.0"?>
<!--
  ~ Copyright © BrainActs Commerce OÜ. All rights reserved.
  ~ See LICENSE.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="unlock" frontName="unlock">
            <module name="Vendor_Module"/>
        </route>
    </router>
</config>

updated Unlock.php file Code


namespace Vendor\Module\Block\Adminhtml\SalesRule\Edit\Button;
use Magento\Catalog\Block\Adminhtml\Product\Edit\Button\Generic;
class Unlock extends Generic
{
    protected $request;
    protected $urlBuilder;

    public function __construct(
        \Magento\Framework\App\Request\Http $request,
        \Magento\Framework\UrlInterface $urlBuilder
    ) {
       $this->request = $request;
       $this->urlBuilder = $urlBuilder;
       }


    public function getUnlockUrl($id)
    {
        return $this->getUrl('unlock/index/unlock');
    }

    public function getButtonData()
    {
        $id = (int)$this->request->getParam('id');
        if ($id) {
            return [
                'label' => __('Unlock'),
                'on_click' =>  'deleteConfirm(' . json_encode(__('Are you sure you want to Unlock?'))
                    . ','
                    . json_encode($this->getUnlockUrl($id))
                    . ')',
                'sort_order' => 100
            ];
        }
    }
  • Thank you. Sorry but I've not understood... giving mine/your code, what the final url will be? – Glen Runciter Oct 16 at 17:26
  • now it works. Thank you – Glen Runciter Oct 16 at 21:40
0

Try this: For Admin Route

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

For Button

<?php 
namespace Vendor\Module\Block\Adminhtml\SalesRule\Edit\Button;
use Magento\Catalog\Block\Adminhtml\Product\Edit\Button\Generic;
class Unlock extends Generic
{
    protected $request;
    protected $urlBuilder;

    public function __construct(
        \Magento\Framework\App\Request\Http $request,
        \Magento\Framework\UrlInterface $urlBuilder
    ) {
       $this->request = $request;
       $this->urlBuilder = $urlBuilder;
       }


    public function getButtonData()
    {
        $id = (int)$this->request->getParam('id');
        if ($id) {
            return [
                'label' => __('Unlock'),
                'on_click' => 'deleteConfirm(\'' . __(
                        'Are you sure you want to do this?'
                    ) . '\', \'' . $this->urlBuilder->getUrl('*/*/unlock', ['id' => $ruleId]) . '\')',
                'sort_order' => 100
            ];
        }
    }
}

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.