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.
Best Answer
routers.xml
should beroutes.xml
. See this file as example