Magento 2 — How to define the admin route, controller, and ACL?

--

hellomage.com

Let’s create it as a module.

  1. start with creating registration.php and module.xml

create a registration.php file in the following path {app/code/HelloMage/DeleteCreditmemo/}

<?php

\Magento\Framework\Component\ComponentRegistrar::register(
\Magento\Framework\Component\ComponentRegistrar::MODULE,
'HelloMage_DeleteCreditmemo',
__DIR__
);

create a module.xml file in the following path {app/code/HelloMage/DeleteCreditmemo/etc/}

<?xml version="1.0"?>
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:noNamespaceSchemaLocation="urn:magento:framework:Module/etc/module.xsd">
<module name="HelloMage_DeleteCreditmemo" setup_version="1.0.0"/>
</config>

2. Now we need to register the route. create the routes.xml inside the following path {app/code/HelloMage/DeleteCreditmemo/etc/adminhtml/}

here the route I named as hellomage

<?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="hellomage" frontName="hellomage">
<module name="HelloMage_DeleteCreditmemo" />
</route>
</router>
</config>

3. Create ACL ( permission ) file acl.xml

Access Control List (ACL) rules allow an admin to limit the permissions of users in Magento. For example, you can use ACL rules to authorize the users to access menus, controllers, etc.

<?xml version="1.0"?>
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:noNamespaceSchemaLocation="urn:magento:framework:Acl/etc/acl.xsd">
<acl>
<resources>
<resource id="Magento_Backend::admin">
<resource id="HelloMage_DeleteCreditmemo::hellomage_extensions"
title="HelloMage Extensions"
sortOrder="100">
<resource id="HelloMage_DeleteCreditmemo::delete_creditmemo"
title="Delete Creditmemo"
sortOrder="80"/>
</resource>
</resource>
</resources>
</acl>
</config>

4. Create an admin controller Creditmemo.php

This will be accessible using “/hellomage/delete/creditmemo” this URL.

<?php

namespace HelloMage\DeleteCreditmemo\Controller\Adminhtml\Delete;

use Magento\Backend\App\Action;

/**
* Class Creditmemo
*
@package HelloMage\DeleteCreditmemo\Controller\Adminhtml\Delete
*/
class Creditmemo extends Action
{
/**
* Creditmemo constructor.
*
@param Action\Context $context
*/
public function __construct(
Action\Context $context
) {
parent::__construct($context);
}

/**
*
@return \Magento\Framework\App\ResponseInterface|\Magento\Framework\Controller\Result\Redirect|\Magento\Framework\Controller\ResultInterface
*/
public function execute()
{
// write your logic here.
echo "HelloMage";
exit;
}

/*
* Check permission via ACL resource
*/

protected function _isAllowed()
{
return $this->_authorization->isAllowed('HelloMage_DeleteCreditmemo::delete_creditmemo');
}
}

Once this set, we can make a call to this controller. Make sure, this can be accessible by using a form key.

http://{yourdomain}/{your_admin_key}/hellomage/delete/creditmemo/key/97c9c30d0bfcee3e90e00be10e3a327db5bc08b1aa6fb6214d357a9c494d98d9/

--

--

No responses yet