| name | create-backend-controller |
| description | Creates a backend (adminhtml) controller action in Magento 2 with proper ACL, routing, authorization, and admin UI integration. Use when building admin pages, AJAX endpoints, form handlers, or mass actions. |
Create Backend (Adminhtml) Controller Action
Description
This skill guides you through creating a backend controller action in Adobe Commerce/Magento 2 (Mage-OS) for the admin area. Backend controllers handle HTTP requests in the Magento admin panel with proper authorization and ACL (Access Control List) integration.
When to Use
- Creating custom admin pages or sections
- Building AJAX endpoints for admin UI components
- Implementing admin form submission handlers
- Creating mass actions for grid components
- Building custom admin operations requiring authorization
Prerequisites
- Existing Magento 2 module with proper structure
- Understanding of ACL (Access Control List) system
- Knowledge of Magento routing and dependency injection
- Understanding of admin sessions and authorization
Best Practices from Adobe Documentation
1. Extend Backend Action Base Class
Backend controllers should extend \Magento\Backend\App\Action:
class ActionName extends \Magento\Backend\App\Action implements HttpGetActionInterface
2. Implement HTTP Method-Specific Interfaces
Always implement HTTP method-specific action interfaces:
HttpGetActionInterface - For GET requests
HttpPostActionInterface - For POST requests
- Both interfaces can be implemented for endpoints accepting multiple methods
3. Define ACL Resource Constant
Every backend controller must define the ADMIN_RESOURCE constant:
const ADMIN_RESOURCE = 'Vendor_Module::resource_name';
4. Use Strict Types
Always declare strict types at the top of controller files:
declare(strict_types=1);
5. Authorization is Automatic
The \Magento\Backend\App\Action base class automatically checks the ADMIN_RESOURCE constant against the current admin user's permissions via the _isAllowed() method.
Step-by-Step Implementation
Step 1: Define ACL Resources (acl.xml)
Create etc/acl.xml to define access control resources:
<?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="Vendor_Module::menu" title="Module Name" sortOrder="100">
<resource id="Vendor_Module::entity" title="Manage Entities" sortOrder="10">
<resource id="Vendor_Module::entity_save" title="Save Entity" sortOrder="10" />
<resource id="Vendor_Module::entity_delete" title="Delete Entity" sortOrder="20" />
</resource>
ACL Resource Structure:
- Each resource has a unique ID (e.g.,
Vendor_Module::entity_save)
- Resources are hierarchical - child resources inherit parent permissions
- Admin users must have permission for the resource to access the controller
Step 2: Create Backend Routes (routes.xml)
Define your route configuration in 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="vendormodule" frontName="vendormodule">
<module name="Vendor_Module" before="Magento_Backend" />
</route>
</router>
</config>
URL Structure: https://yourdomain.com/admin/{frontName}/{controller}/{action}
Example: With frontName vendormodule, the URL would be:
https://yourdomain.com/admin/vendormodule/entity/index
Step 3: Create Admin Menu (menu.xml) [Optional]
Create etc/adminhtml/menu.xml to add menu items:
<?xml version="1.0"?>
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:noNamespaceSchemaLocation="urn:magento:module:Magento_Backend:etc/menu.xsd">
<menu>
<add id="Vendor_Module::menu"
title="Module Name"
module="Vendor_Module"
sortOrder="100"
resource="Vendor_Module::menu"/>
<add id="Vendor_Module::entity"
title="Manage Entities"
module="Vendor_Module"
sortOrder="10"
parent="Vendor_Module::menu"
action="vendormodule/entity/index"
resource="Vendor_Module::entity"/>
<add id="Vendor_Module::settings"
title="Settings"
module="Vendor_Module"
sortOrder="20"
=
=
=/>
Step 4: Create Controller Directory Structure
Create the controller directory:
app/code/Vendor/ModuleName/Controller/Adminhtml/
└── ControllerName/
└── ActionName.php
Example: Controller/Adminhtml/Entity/Index.php maps to URL: /admin/vendormodule/entity/index
Step 5: Create Backend Controller Action Class
Example 1: Admin Grid Page Controller
<?php
declare(strict_types=1);
namespace Vendor\Module\Controller\Adminhtml\Entity;
use Magento\Backend\App\Action;
use Magento\Backend\App\Action\Context;
use Magento\Framework\App\Action\HttpGetActionInterface;
use Magento\Framework\View\Result\PageFactory;
use Magento\Framework\View\Result\Page;
class Index extends Action implements HttpGetActionInterface
{
const ADMIN_RESOURCE = 'Vendor_Module::entity';
private PageFactory ;
{
::();
->resultPageFactory = ;
}
{
= ->resultPageFactory->();
->();
->()->()->(());
;
}
}
Example 2: JSON Response Controller (AJAX Endpoint)
<?php
declare(strict_types=1);
namespace Vendor\Module\Controller\Adminhtml\Entity;
use Magento\Backend\App\Action;
use Magento\Backend\App\Action\Context;
use Magento\Framework\App\Action\HttpGetActionInterface;
use Magento\Framework\App\Action\HttpPostActionInterface;
use Magento\Framework\Controller\Result\JsonFactory;
use Magento\Framework\Controller\ResultInterface;
use Vendor\Module\Model\ResourceModel\Entity\CollectionFactory;
class Search extends Action ,
{
= ;
JsonFactory ;
CollectionFactory ;
{
::();
->resultJsonFactory = ;
->collectionFactory = ;
}
{
= ->()->();
= ()->()->(, );
= ()->()->(, );
= ->collectionFactory->();
->(, [ => ]);
->()->();
= ->();
= [];
( ) {
[->()] = [
=> ->(),
=> ->(),
=> ((), ->())
];
}
= ->resultJsonFactory->();
->([
=> ,
=> () ? :
]);
}
}
Example 3: Save Action with Form Key Validation
<?php
declare(strict_types=1);
namespace Vendor\Module\Controller\Adminhtml\Entity;
use Magento\Backend\App\Action;
use Magento\Backend\App\Action\Context;
use Magento\Framework\App\Action\HttpPostActionInterface;
use Magento\Framework\Controller\ResultInterface;
use Magento\Framework\Exception\LocalizedException;
use Vendor\Module\Api\EntityRepositoryInterface;
use Vendor\Module\Model\EntityFactory;
class Save extends Action implements HttpPostActionInterface
{
= ;
EntityFactory ;
EntityRepositoryInterface ;
{
::();
->entityFactory = ;
->entityRepository = ;
}
{
= ->resultRedirectFactory->();
= ->()->();
(!) {
->messageManager->(());
->();
}
{
= ->()->();
() {
= ->entityRepository->();
} {
= ->entityFactory->();
}
->();
->entityRepository->();
->messageManager->(());
(->()->()) {
->(, [ => ->()]);
}
->();
} (LocalizedException ) {
->messageManager->(->());
} (\ ) {
->messageManager->(
,
()
);
}
->(, [ => ?? ]);
}
}
Example 4: Mass Action Controller
<?php
declare(strict_types=1);
namespace Vendor\Module\Controller\Adminhtml\Entity;
use Magento\Backend\App\Action;
use Magento\Backend\App\Action\Context;
use Magento\Framework\App\Action\HttpPostActionInterface;
use Magento\Framework\Controller\ResultFactory;
use Magento\Framework\Controller\ResultInterface;
use Magento\Framework\Exception\LocalizedException;
use Vendor\Module\Api\EntityRepositoryInterface;
use Vendor\Module\Model\ResourceModel\Entity\CollectionFactory;
use \\\\;
{
= ;
Filter ;
CollectionFactory ;
EntityRepositoryInterface ;
{
::();
->filter = ;
->collectionFactory = ;
->entityRepository = ;
}
{
{
= ->filter->(->collectionFactory->());
= ;
( ) {
->entityRepository->();
++;
}
->messageManager->(
(, )
);
} (LocalizedException ) {
->messageManager->(->());
} (\ ) {
->messageManager->(
,
()
);
}
= ->resultFactory->(::);
->();
}
}
Example 5: Delete Action
<?php
declare(strict_types=1);
namespace Vendor\Module\Controller\Adminhtml\Entity;
use Magento\Backend\App\Action;
use Magento\Backend\App\Action\Context;
use Magento\Framework\App\Action\HttpPostActionInterface;
use Magento\Framework\Controller\ResultInterface;
use Magento\Framework\Exception\LocalizedException;
use Vendor\Module\Api\EntityRepositoryInterface;
class Delete extends Action implements HttpPostActionInterface
{
const ADMIN_RESOURCE = 'Vendor_Module::entity_delete';
EntityRepositoryInterface ;
{
::();
->entityRepository = ;
}
{
= ->resultRedirectFactory->();
= ->()->();
(!) {
->messageManager->(());
->();
}
{
->entityRepository->(());
->messageManager->(());
} (LocalizedException ) {
->messageManager->(->());
} (\ ) {
->messageManager->(
,
()
);
}
->();
}
}
Step 6: Create Layout XML
Create layout XML: view/adminhtml/layout/vendormodule_entity_index.xml
<?xml version="1.0"?>
<page xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:noNamespaceSchemaLocation="urn:magento:framework:View/Layout/etc/page_configuration.xsd">
<update handle="styles"/>
<body>
<referenceContainer name="content">
<uiComponent name="vendor_module_entity_listing"/>
</referenceContainer>
</body>
</page>
Step 7: Clear Cache and Test
ddev exec bin/magento cache:flush
ddev exec bin/magento setup:upgrade
ddev exec bin/magento setup:di:compile
Common Patterns
Pattern 1: Inline Edit (AJAX Save)
public function execute(): ResultInterface
{
$resultJson = $this->resultJsonFactory->create();
$items = $this->getRequest()->getParam('items', []);
if (empty($items)) {
return $resultJson->setData([
'messages' => [__('Please correct the data sent.')],
'error' => true
]);
}
foreach ($items as $entityId => $entityData) {
try {
$entity = $this->entityRepository->getById($entityId);
$entity->setData(array_merge($entity->getData(), $entityData));
$this->entityRepository->save($entity);
} catch (\Exception $e) {
return ->([
=> [->()],
=>
]);
}
}
->([
=> [()],
=>
]);
}
Pattern 2: Custom Authorization Check
protected function _isAllowed(): bool
{
$isAllowed = $this->_authorization->isAllowed('Vendor_Module::entity');
if ($isAllowed && $this->getRequest()->getParam('special_flag')) {
$isAllowed = $this->_authorization->isAllowed('Vendor_Module::special_permission');
}
return $isAllowed;
}
Pattern 3: File Upload in Admin Form
public function execute(): ResultInterface
{
$data = $this->getRequest()->getPostValue();
if (isset($_FILES['image']) && $_FILES['image']['name']) {
try {
$uploader = $this->uploaderFactory->create(['fileId' => 'image']);
$uploader->setAllowedExtensions(['jpg', 'jpeg', 'gif', 'png']);
$uploader->setAllowRenameFiles(true);
$uploader->setFilesDispersion(true);
$result = $uploader->save(
$this->mediaDirectory->getAbsolutePath('vendor_module/entity/')
);
$data['image'] = 'vendor_module/entity' . $result['file'];
} catch (\Exception ) {
->messageManager->(->());
}
}
}
Testing Admin Controllers
Unit Test Example
Create: Test/Unit/Controller/Adminhtml/Entity/SaveTest.php
<?php
declare(strict_types=1);
namespace Vendor\Module\Test\Unit\Controller\Adminhtml\Entity;
use PHPUnit\Framework\TestCase;
use Vendor\Module\Controller\Adminhtml\Entity\Save;
class SaveTest extends TestCase
{
public function testExecuteWithValidData(): void
{
$context = $this->createMock(\Magento\Backend\App\Action\Context::class);
$entityFactory = $this->createMock(\Vendor\Module\Model\EntityFactory::class);
$entityRepository = $this->createMock(\Vendor\Module\Api\EntityRepositoryInterface::class);
$controller = (, , );
}
}
Troubleshooting
Issue: Access Denied (403)
- Check ACL resource is defined in
etc/acl.xml
- Verify
ADMIN_RESOURCE constant matches ACL resource ID
- Ensure admin user role has permission for the resource
- Run
ddev exec bin/magento cache:flush
- Check Stores > Configuration > Admin > Admin Base URL
Issue: 404 Not Found
- Verify
routes.xml is in etc/adminhtml/ (not etc/frontend/)
- Check frontName is unique and doesn't conflict
- Ensure controller extends
\Magento\Backend\App\Action
- Run
ddev exec bin/magento setup:upgrade
Issue: Form Key Validation Failed
- Ensure form includes form key:
<?= $block->getFormKey() ?>
- POST requests automatically validate form keys
- For AJAX, include form key in data
Issue: Menu Not Showing
- Check
menu.xml is in etc/adminhtml/
- Verify ACL resource permissions
- Clear admin cache:
ddev exec bin/magento cache:clean config
- Check admin user has permission to resource
Security Best Practices
- Always Define ACL Resources: Never use
const ADMIN_RESOURCE = 'Magento_Backend::admin' for production controllers
- Validate Input: Use input validators and filters
- Use Form Keys: Magento automatically validates form keys for POST requests
- Escape Output: Use
$escaper->escapeHtml() in templates
- Check Permissions: Let
_isAllowed() handle authorization
- Use Type Hints: Ensure strict types are declared
- Log Sensitive Actions: Use logger for delete/update operations
References
NTOTanks-Specific Notes
- Follow PSR-12 coding standards
- Use
ddev exec prefix for all Magento CLI commands
- Backend controllers integrate with Hyvä Admin module for UI components
- Test admin controllers after clearing cache and recompiling
- Check admin user permissions in System > User Roles