Creating a Multi-Page Magento Admin Module: A Comprehensive Guide
Are you looking to extend your Magento backend functionality by creating a custom admin module? I’ll walk you through the process of building an admin module with multiple pages - an overview page and a form to create items.
Understanding Magento Admin Modules
Magento relies on modules for its core and extension functionality. An admin module allows you to add or modify features specifically designed for store administrators. The structure is similar to a regular Magento module but requires specific configuration for the admin configuration.
Setting Up Your Development Environment
Before we dive into coding, ensure your development environment is ready:
- Have Magento installed and running on your local server.
- Set up an FTP client (like FileZilla) to upload files.
- Create a new folder named
app/code/Vendor/YourModulein your Magento root directory.
Creating the Overview Page
Let’s start by creating the overview page for our admin module.
- Under
Vendor/YourModule/etc/adminhtml, create aroutes.xmlfile:
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:App/etc/routes.xsd">
<router id="admin">
<route frontName="your_module" id="your_module" />
</router>
</config>
Replace your_module with the name of your module.
- In
Vendor/YourModule/Controller/Adminhtml/Index, create anIndex.phpfile:
namespace Vendor\YourModule\Controller\Adminhtml\Index;
class Index extends \Magento\Backend\App\Action
{
public function execute()
{
$this->_view->loadLayout();
$this->_setActiveMenu('Vendor_YourModule::index');
$this->getMessageManager()->addSuccess(__('Hello, This is custom Vendor YourModule'));
$this->_addContent($this->_view->getLayout()->createBlock(
'Magento\Framework\View\Element\Template'
)->setTemplate('Vendor_YourModule::my_template.phtml'));
return $this->_view->getResponse();
}
}
- Create a
my_template.phtmlfile underVendor/YourModule/view/adminhtml/templates:
<div class="page-header">
<h1><?php echo __('My Custom Page'); ?></h1>
</div>
- Update your module’s
etc/module.xml:
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:Module/etc/module.xsd">
<module name="Vendor_YourModule" setup_version="1.0.0">
<sequence>
<module name="Magento_Backend"/>
</sequence>
</module>
</config>
Creating the Form to Create an Item
To create a form, follow these steps:
- In
Vendor/YourModule/etc/adminhtml/menu.xml, add the following code:
<?xml version="1.0"?>
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:module:Magento_Adminhtml:etc/menu.xsd">
<menu>
<add id="Vendor_YourModule::index"
title="My Custom Module"
module="Vendor_YourModule"
sortOrder="50"
resource="Vendor_YourModule::your_module" />
</menu>
</config>
- In
Vendor/YourModule/Controller/Adminhtml/Index/Save.php, create a save action:
namespace Vendor\YourModule\Controller\Adminhtml\Index;
class Save extends \Magento\Backend\App\Action implements \Magento\Backend\Model\Auth\AuthorizationInterface
{
// Implementation here...
}
- Create the form in
Vendor/YourModule/view/adminhtml/templates/save.phtml:
<form action="<?php echo $block->getSaveUrl(); ?>" method="post" id="your_module_form">
<!-- Your form fields go here -->
</form>
Finalizing and Activating Your Extension
To complete the process:
- Flush Magento cache.
- Navigate to
System > Configurationin the backend. - Click on
AdvancedunderMagento Company. - In the
Developertab, setEnable Developer ModetoYes. - Set
DebugandTemplate Path HintstoYes. - Log out and log back in.
You should now see your custom admin menu item in the Magento backend navigation.
Remember, building an admin module requires a thorough understanding of Magento’s architecture and OOP principles. For more detailed examples, check out the official Magento 2 documentation or explore Laravel Company’s guide on Magento development best practices.
Happy coding!