1

I have created the custom menu in the admin panel. When I click on menu option then redirect to dashboard.

My Magento v2.0.17

Menu

<?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="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"/>
</menu>

Route

<?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="contacts" frontName="contacts">
        <module name="Pfay_Contacts" before="Magento_Backend"/>
    </route>
</router>

Block Page

<?php
namespace Pfay\Contacts\Block;
use Magento\Framework\View\Element\Template;

class Contactslist extends \Magento\Framework\View\Element\Template
{
public function __construct(Template\Context $context, array $data = array())
{
    parent::__construct($context, $data);
    $this->setData('contacts',array());
}

public function addContacts($count)
{
    $_contacts = $this->getData('contacts');
    $actualNumber = count($_contacts);
    $names = array();
    for($i=$actualNumber;$i<($actualNumber+$count);$i++) {
        $_contacts[] = 'nom '.($i+1);
    }
    $this->setData('contacts',$_contacts);
}
}

Controller

<?php
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');
}
}
6
  • you forget to paste the proper code here or you are missed to add forward slash \ before Template\Context $context in __construct param?
    – Hitesh
    Oct 3, 2018 at 7:50
  • Hi @Hit's code is correct. We do not need the forward slash before Template\Context Oct 3, 2018 at 7:57
  • yes right just checking it, as I don't have enough time to check your code please check here sample module of admin menu github.com/hitesh-srtpl/magento225-demo/tree/master/app/code/…
    – Hitesh
    Oct 3, 2018 at 7:58
  • Ok...I will check and Update you Oct 3, 2018 at 8:01
  • I have checked. It is working fine. Thanks @Hit's Oct 3, 2018 at 8:17

2 Answers 2

2

If you want to create an admin menu in admin side, then please follow this link

and later you can compare your code and above module code to check whether you did a mistake.

0

You have missing Acl which manage admin access area permission.

Create acl.xml at app/code/Pfay/Contacts/etc/ and you have to define the ACL for this module URLs.

If you check menu.xml you will find out you have use resource resource="Pfay_Contacts::contacts" but does not define the resource.

<?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">
                <!-- Menu -->
                <resource id="Pfay_Contacts::contacts" title="Pierre FAY Contacts" translate="title" sortOrder="100">
                    <resource id="Pfay_Contacts::index" title="Pierre FAY Contacts" translate="title" sortOrder="101">
                    </resource>
                </resource>
            </resource>
        </resources>
    </acl>
</config>

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="Pfay_Contacts::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" parent="Pfay_Contacts::contacts"/>
</menu>

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.