Wishusucess AdminMenu: How to Create Magento 2 Admin Menu Module

Here you will see how we can create Magento 2 Wishusucess AdminMenu Module, and also you will get to know how to add a link to the Magento 2 Admin Menu, which is shown on the left side of the Admin Menu pages of Magento 2.

 

Structure of the Magento 2 Admin Menu

How Magento 2 menu item performs an action on each menu item.

Here we have created a menu which is Wishusucess and that has a two-item level menu which is Admin Menu One and the second menu item which is Admin Menu Two.

Wishusucess AdminMenu
Wishusucess AdminMenu Item

 

Folder Structure of Wishusucess AdminMenu

Magento_2_Root_Direcotry/app/code/

Wishusucess/AdminMenu/registration.php

Wishusucess/AdminMenu/etc/module.xml

Wishusucess/AdminMenu/etc/adminhtml/menu.xml

So you need only three files to create an Admin Menu item.

Now let's see how that file we have to write.

 

Step 1: Register Your Magento 2 Module

First, we have to register our module through the following file.

Wishusucess/AdminMenu/registration.php

<?php
\Magento\Framework\Component\ComponentRegistrar::register(
\Magento\Framework\Component\ComponentRegistrar::MODULE,
'Wishusucess_AdminMenu',
__DIR__
);

 

Step 2: Give the Basic Information of Module

Wishusucess/AdminMenu/etc/module.xml

<?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="Wishusucess_AdminMenu" setup_version="1.0.0">
</module>
</config>

 

Step 3: Create Magento 2 AdminMenu Item

Wishusucess/AdminMenu/etc/adminhtml/menu.xml

<?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="Wishusucess_AdminMenu::adminmenu" title="Wishusucess" module="Wishusucess_AdminMenu" sortOrder="51" resource="Wishusucess_AdminMenu::adminmenu"/>
<add id="Wishusucess_AdminMenu::one" title="Admin Menu One" module="Wishusucess_AdminMenu" sortOrder="10" action="wishusucess_adminmenu/one" resource="Wishusucess_AdminMenu::one" parent="Wishusucess_AdminMenu::adminmenu"/>
<add id="Wishusucess_AdminMenu::two" title="Admin Menu Two" module="Wishusucess_AdminMenu" sortOrder="99" parent="Wishusucess_AdminMenu::adminmenu" action="adminhtml/system_config/edit/section/adminmenu" resource="Wishusucess_AdminMenu::adminmenu_configuration"/>
</menu>
</config>

 

Now Run The Following Command to See Wishusucess AdminMenu:

php bin/magento setup:upgrade

php bin/magento setup:di:compile

php bin/magento setup:static-content:deplpy -f

php bin/magento cache:clean

 

Magento 2 AdminMenu Attribute Definitions

Now the basic attribute which we need to understand.

The id: Identifier or It’s a unique string

format: {Vendor_ModuleName}::{menu_description}.

The title: Text which will be shown on the menu bar.

The module: Menu is belong to.

The sortOrder: Defined the position of the menu.

Priority Order: 0 Means top priority

  • 10: Highest Priority
  • 20: Second Highest
  • 30: Third Highest
  • and so on...

The parent: This decides which is the parent of other items.

For example, we have parent=”Wishusucess_AdminMenu::adminmenu”, so we - know this menu “Admin Menu One” is a child of “Wishusucess” menu and it will show inside of Wishusucess menu.

The action: Define the URL of the page to which this menu links.

format: {router_name}{controller_folder}{action_name}.

In this example, this menu will link to the module Wishusucess, controller one, and action Index

The resource: Defined the Access Control Language (ACL) rule which the admin user must have in order to see and access this menu.

 

AdminMenu Module Download Link:

AdminMenu GitHub

Magento 2.4 Installation Guide: How to Install Magento 2.4.2

 

Related Post:

HelloWorld Extension: How to Create Hello World Module

 

HelloWorld Extension: How to Create Hello World Module

Here, I am going to explain how easily we can create Magento 2 HelloWorld Extension.

The following directory structure we have to follow in order to create our Magento 2 module.

Magento Installation

Directory/app/code/Vender_Name/Module_Name

or

Root_directory/app/code/Vender_Name/Module_Name

Here...

Root_directory/app/code/Wishusucess/HelloWorld

 

Follow the below steps to create the HELLO WORLD module

The modules have a 2 part naming structure, i.e,

Namespace/Module.

Wishusucess is the vendor name and HelloWorld is the module name.

So now that we’ve established the directory structure, let’s make a simple module that will show “Hello World” on the frontend section when we will open the front view.

 

HelloWorld Extension in Magento 2

 

Url: http://wishusucess.com/helloworld/


So our complete directory structure will be look something like this.

  • Wishusucess/HelloWorld/registration.php
  • Wishusucess/HelloWorld/etc/module.xml
  • Wishusucess/HelloWorld/etc/frontend/routes.xml
  • Wishusucess/HelloWorld/Controller/Index/Index.php
  • Wishusucess/HelloWorld/Block/Hello.php
  • Wishusucess/HelloWorld/view/frontend/layout/helloworld_index_index.xml
  • Wishusucess/HelloWorld/view/frontend/templates/hello.phtml

 

Magento 2 HelloWorld Extension Basic File

So let's start with the registration.php file.

  • Wishusucess/HelloWorld/registration.php
<?php
\Magento\Framework\Component\ComponentRegistrar::register(
\Magento\Framework\Component\ComponentRegistrar::MODULE,
'Wishusucess_HelloWorld',
__DIR__
);

 

This is one of the first files which needs to register our module with Magento 2. Every module must contain this file otherwise extension will not get registered with the root.

 

Now the second most important file is module.xml that just contains some basic information about the module. Like the module name and module version.

Wishusucess/HelloWorld/etc/module.xml

<?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="Wishusucess_HelloWorld" setup_version="1.0.0">
</module>
</config>

 

Routes in HelloWorld Extension

If you want to give a path then it is necessary to create routes.xml file

We have added it in the frontend/ folder because it is frontend access root if we need to access in adminhtml side then we need to add it to adminhtml/ folder

Content would be:

Wishusucess/HelloWorld/etc/frontend/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="standard">
<route id="helloworld" frontName="helloworld">
<module name="Wishusucess_HelloWorld" />
</route>
</router>
</config>

 

Now, the controller actually contains where the request would be routed.

Wishusucess/HelloWorld/Controller/Index/Index.php

<?php
/**
*
* @package magento2
* @author Wishusucess Magento Team
* @license http://wishusucess.com/licenses/OSL-3.0 Open Software License v. 3.0 (OSL-3.0)
* @link http://www.wishusucess.com/
*/
namespace Wishusucess\HelloWorld\Controller\Index;

use Magento\Framework\App\Action\Action;
use Magento\Framework\App\Action\Context;
use Magento\Framework\App\ResponseInterface;
use Magento\Framework\View\Result\PageFactory;

class Index extends Action {
/**
* @var PageFactory
*/
private $pageFactory;


/**
* Index constructor.
* @param Context $context
* @param PageFactory $pageFactory
*/
public function __construct(
Context $context,
PageFactory $pageFactory
)
{
parent::__construct($context);
$this->pageFactory = $pageFactory;
}


/**
* Execute action based on request and return result
*
* Note: Request will be added as operation argument in future
*
* @return \Magento\Framework\Controller\ResultInterface|ResponseInterface
* @throws \Magento\Framework\Exception\NotFoundException
*/
public function execute()
{
$page = $this->pageFactory->create();
return $page;
}
}

 

Now to block file we have to create which contains the block's function within Magento's MVC architecture is to structure and provide data to the template which the template will then display

Wishusucess/HelloWorld/Block/Hello.php

<?php
/**
*
* @package magento2
* @author Wishusucess Magento Team
* @license http://wishusucess.com/licenses/OSL-3.0 Open Software License v. 3.0 (OSL-3.0)
* @link http://www.wishusucess.com/
*/

namespace Wishusucess\HelloWorld\Block;

use Magento\Framework\View\Element\Template;

class Hello extends Template
{
public function getText() {
return "Hello World";
}
}

 

In Magento 2 MVC structure view/layout file has to handle the request.

Wishusucess/HelloWorld/view/frontend/layout/helloworld_index_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">
<body>
<referenceContainer name="content">
<block class="Wishusucess\HelloWorld\Block\Hello" name="helloworld" template="Wishusucess_HelloWorld::hello.phtml" />
</referenceContainer>
</body>
</page>

 

Now template file shows the data on the frontend.

Wishusucess/HelloWorld/view/frontend/templates/hello.phtml

<?php
/* @var \Wishusucess\HelloWorld\Block\Hello $block */
?>
<label><?= __("This data returns from block function getText() ") ?></label>
<h1><?= $block->getText() ?></h1>

 

Now run the following command:

php bin/magento setup:upgrade 

php bin/magento setup:di:compile 

php bin/magento setup:static-content:deploy -f

php bin/magento cache:clean

Now go to browser and type something like this: yourstore.com/helloworld/

you will see output like the below screenshot.

Magento 2 HelloWorld Extension
Magento 2 HelloWorld Extension

 

Download Code:

Wishusucess/HelloWorld

 

Related Post:

Magento 2.4 Installation Guide: How to Install Magento 2.4.2

Elasticsearch: How to Install Elasticsearch in Ubuntu 18.04

 

Suggested Post:

SEO Packages: How Much Do SEO Packages Cost in India, SEO Pricing