Create an admin interface under magento2

This tutorial is the 6th in a long series of magento2 tutorial. It will be updated as the platform evolves. You will now learn how to create a controller for the backend of magento2 ie to add your module in the administration of magento2. If you do not know magento (v1) it does not matter, I'll talk about it of course but it is not necessary to know the magento platform (v1) to master this tutorial on magento2.

In the previous tutorial we saw how to create an install under magento2 for our extension! We will see how to add our module an administration interface. For that, we will start from the files of our previous tutorial.

Add an entry to the magento2 administration menu

Go to the / etc directory of your module and add an adminhtml/ this folder that will contain the configuration files of your module concerning the backoffice. Add a menu.xml file with the following content:

<?xml version="1.0"?>
<config xmlns:xsi="" xsi:noNamespaceSchemaLocation="urn:magento:module:Magento_Backend:etc/menu.xsd">
        <add id="Pfay_Contacts" title="Pierre FAY Contacts" module="Pfay_Contacts" sortOrder="20" dependsOnModule="Pfay_Contacts" resource="Pfay_Contacts::contacts"/>
        <add id="Pfay_Contacts::index" title="Pierre FAY Contacts" module="Pfay_Contacts" action="contacts/test/index" parent="Pfay_Contacts" sortOrder="20" dependsOnModule="Pfay_Contacts" resource="Pfay_Contacts::contacts"/>

In the config, add in "menu", an "add" tag that will allow you to simply manage this menu with its attributes:
- id is the unique identifier of the menu entry
- title is the title of your tab, this is what will appear in the backoffice
- module indicates which module is the link to the menu entry
- action makes it possible to indicate on which action of the module one will be redirected when one clicks on this entry of the menu. Here we will be redirected to the action contacts/test/index of the administration part of the module.
- parent must be the id of another "add" tag in the menu, it is the parent of the menu entry, ie this entry will be "below" Of his relative in the menu
- sortOrder will allow you to prioritize some tab to display for tabs that have the same parent.
- dependsOnModule allows you to display the tab only if the specified module is present.

Declare the adminhtml route for your magento2 module

Connect to your administration interface and you will see the tab appear in the backoffice but when you click below it is a 404 because as on the front office, a road needs to be defined to be able to find the controller Of the backoffice for this module.
So create the routes.xml file in your adminhtml / folder in your / etc / module folder with the following contents:

<?xml version="1.0"?>
<config xmlns:xsi="" xsi:noNamespaceSchemaLocation="urn:magento:framework:App/etc/routes.xsd">
    <router id="admin">
        <route id="contacts" frontName="contacts">
            <module name="Pfay_Contacts" />

Through this route you will be able to access the controller of the administration part of your module.

Create your magento2 admin controller

To create this controller, go to the Controller/ folder of your module, create the Adminhtml/ folder with a test/ Index.php with the following content:

namespace Pfay\Contacts\Controller\Adminhtml\Test;
use Magento\Backend\App\Action;

class Index extends \Magento\Backend\App\Action
    public function execute()
        die('test admin view');

It then "updates" its module and gives the rights to the files if necessary, via the following commands:

    php bin/magento setup:upgrade
    chown www-data:www-data -R .
    chmod 777 -R var/
Go back to your administration interface, refresh the page and you can now click on the menu entry that will redirect you to your index controller of the administration part of your magento2 module.

There you go ! This is the end of this tutorial. Does not it work for you? Retrieve the sources of this module at the bottom of this page and find more easily your error.

You now know add an entry to the magento2 administration menu and create the controller action associated ... Congratulations!

If you liked this tutorial, please thank me in 1 click by sharing this article on twitter, googleplus or facebook, it may seem useless but for me it is very important. Thanks to those who will do it;) Do not hesitate also to ask your questions in the comments (for those who share the article), I will try to answer quickly.
Good luck to all for the continuation of these tutorials on magento2!
Available documents for this article :
Books that can help you :
  • Livre Magento 2 Developer's Guide by Branko
  • Livre Mastering Magento2 Second Edition
  • Livre Magento 2 Cookbook
Questions about this lesson
No questions for this lesson. Be the first !

You must be logged in to ask for help on a lesson.