0

I am learning Magento2 and I have created adminhtml test module it's not working.

Can you please suggest me where i went wrong?

my code is:

app\etc\config.php

'modules' =>
        array (
        'Magento_Test' => 1, 

        ),

app\code\Magento\Test\Block\Adminhtml\Test.php

<?php
namespace Magento\Test\Block\Adminhtml;
class Test extends use \Magento\Framework\View\Element\Template;
{
    public function _prepareLayout(){
        return parent::_prepareLayout();
    }
}

app\code\Magento\Test\Controller\Adminhtml\Index\Index.php

<?php
/**
 * Copyright © 2015 Magento. All rights reserved.
 * See COPYING.txt for license details.
 */
namespace Magento\Test\Controller\Adminhtml\Index;

class Index extends \Magento\Backend\App\Action
{
    public function execute() {
    $this->_view->loadLayout();
    $this->_view->renderLayout();
    }
}

app\code\Magento\Test\etc\module.xml

<?xml version="1.0"?>
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="../../../../../lib/internal/Magento/Framework/Module/etc/module.xsd">
    <module name="Magento_Test" schema_version="2.0.0.1">
    </module>
</config>

app\code\Magento\Test\etc\adminhtml\routes.xml

<?xml version="1.0"?>
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
        xsi:noNamespaceSchemaLocation="../../../../../../lib/internal/Magento/Framework/App/etc/routes.xsd">
    <router id="admin">
        <route id="test" frontName="test" >
            <module name="Magento_Test" />
        </route>
    </router>
</config>

app\code\Magento\Test\etc\acl.xml

<?xml version="1.0"?>
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="../../../../../lib/internal/Magento/Framework/Acl/etc/acl.xsd">
    <acl>
        <resources>
            <resource id="Magento_Adminhtml::admin">
                <resource id="Magento_Test::test" title="Custom Module" sortOrder="40">
                    <resource id="Magento_Test::item" title="New Record" sortOrder="40" />
                </resource>
                <resource id="Magento_Adminhtml::stores">
                    <resource id="Magento_Adminhtml::stores_settings">
                        <resource id="Magento_Adminhtml::config">
                        </resource>
                    </resource>
                </resource>
            </resource>
        </resources>
    </acl>
</config>

app\code\Magento\Test\etc\adminhtml\menu.xml

<?xml version="1.0"?>
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="../../../Backend/etc/menu.xsd">
    <menu>
        <add id="Magento_Test::test" title="test" module="Magento_Test" sortOrder="40" resource="Magento_Test::test"/>
        <add id="Magento_Test::item" title="New Record" module="Magento_Test" sortOrder="40" parent="Magento_Test::test" action="test/index/" resource="Magento_Test::item"/>
    </menu>
</config>

app\code\Magento\Test\view\adminhtml\layout\test_index_index.xml

<?xml version="1.0" encoding="UTF-8"?>
<!--
/**
 * Copyright © 2015 Magento. All rights reserved.
 * See COPYING.txt for license details.
 */
-->
<page xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="../../../../../../../lib/internal/Magento/Framework/View/Layout/etc/page_configuration.xsd">
    <update handle="formkey"/>
    <body>
        <referenceContainer name="content">
            <block class="Magento\Test\Block\Adminhtml\Test" name="test_test" template="test/test.phtml">
            </block>
        </referenceContainer>
    </body>
</page>

app\code\Magento\Test\view\adminhtml\templates\test\test.phtml

<h1>Magento 2 Custom Module </h1>

url: http://localhost/magento-2/admin/test/index/index/

it's redirecting dashboard page while entering above url.

Can you please suggest me where i went wrong?

Thanks.

8
  • Is the module enable in store advance? Did you clear cache (rm -rf var/cache/* var/page_cache/* var/generation/*)? Commented Jul 21, 2015 at 14:03
  • @R.S, Yes I have cleared. Still not executing my module. Is it right my code? or something went wrong? Commented Jul 21, 2015 at 14:13
  • Side note: Please don't use "Magento_Test" as a module name in practice. "Magento_*" is reserved for use by Magento. Use your own vendor name (I use "AlanKent" for my own play projects.)
    – Alan Kent
    Commented Jul 22, 2015 at 4:53
  • Not an answer, but have you looked at github.com/magento/magento2-samples? Might be something useful there to learn from.
    – Alan Kent
    Commented Jul 22, 2015 at 4:56
  • 1
    I copied your files to my tree. I deleted var/*. I got an error talking about the module.xml file (in developer mode) - I changed "schema_version" to "setup_version" and then ran "magento setup:upgrade". I went to the Admin page and selected Store / Configuration / Advanced / Advanced and expanded "disable module output". The Magento_Test module was visible. So its being loaded. I played a bit longer, but had other things to get done. But I certainly came away with ideas for improvements that would make debugging easier!!
    – Alan Kent
    Commented Jul 23, 2015 at 0:50

2 Answers 2

1

routers.xml should be routes.xml. See this file as example

2
  • Still not executing my module. Is it right my code? or something went wrong? Commented Jul 21, 2015 at 14:17
  • Clear the cache after the filename change. If that doesn't work I have no other ideas.
    – Marius
    Commented Jul 21, 2015 at 14:23
0

I've created menu.xml in /view/adminhtml

<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:module:Magento_Backend:etc/menu.xsd"> <menu> <add id="Vendor_MyModule::mymodule_index" title="My Module" module="Vendor_MyModule" sortOrder="0" parent="Magento_Backend::content_elements" action="mymodule/index" resource="Vendor_MyModule::index"/> </menu> </config> I copied this from cms module and updated it with my module information. I can see the item under Content -> Elements in admin 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.