0

I have doubt as how to create Backend Module using block,layout and templates. After creating the block,I have assigned the block in app_appdashboard_index.xml file and I called the block page function in Template file(appdashboard.phtml). I didn't get the data from the Block function. It also makes my page empty

Please find the below code for creating Backend module

File path:app/code/VendorName/ModuleName/view/Adminhtml/layout/app_appdashboard_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">
    <head>
        <title>
            My App
        </title>
    </head>
    <body>
        <referenceContainer name="content">
            <block class="VendorName\ModuleName\Block\Dashboard" template="VendorName_ModuleName::appdashboard.phtml"/>
        </referenceContainer>
    </body>
</page>

File path:app/code/VendorName/ModuleName/Block/Adminhtml/Dashboard.php

<?php
namespace VendorName\ModuleName\Block\Adminhtml;
class Dashboard extends \Magento\Backend\Block\Template
{
    public function __construct(\Magento\Backend\Block\Template\Context $context)
    {
        parent::__construct($context);
    }

    public function sayHello()
    {
        return __('Hello World');
    }
}

File path:app/code/VendorName/ModuleName/view/Adminhtml/templates/appdashboard.phtml

<?php
echo $block->sayHello();
?>

Help me as how to get data from block function to template phtml file

Thanks in Advance :)

Regards,

Hari

  • Have you created the controller as well? – Ziru Zanrgeiff Jan 22 at 20:00
0

First you have to craete admin Route file

app/code/VendorName/ModuleName/etc/adminhtml

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="admin">
        <route id="[route_name]" frontName="[route_name]">
            <module name="VendorName_ModuleName" />
        </route>
    </router>
</config>

Now you have to create Controller file which control your route

app/code/VendorName/ModuleName/Controller/Adminhtml/AppDashboard

index.php

<?php

namespace VendorName\ModuleName\Controller\Adminhtml\AppDashboard;

class Index extends \Magento\Backend\App\Action
{
    private $resultPageFactory;

    public function __construct(
        \Magento\Backend\App\Action\Context $context,
        \Magento\Framework\View\Result\PageFactory $resultPageFactory
    ) {
        parent::__construct($context);
        $this->resultPageFactory = $resultPageFactory;
    }
    public function execute()
    {
        $resultPage = $this->resultPageFactory->create(); // this crete an empty page 
        $resultPage->getConfig()->getTitle()->prepend(__('Page Created'));//this is your page heading 
        return $resultPage;// this show page
    }
}

Now we are create an Block file

app/code/VendorName/ModuleName/Block/Adminhtml

Dashboard.php

<?php

namespace VendorName\ModuleName\Block\Adminhtml;

class Dashboard extends \Magento\Backend\Block\Template
{
    public function __construct(\Magento\Backend\Block\Template\Context $context)
    {
        parent::__construct($context);
    }

    public function sayHello()
    {
        $txt = 'Hello World';
        return $txt;
    }
}

Now Create a phtml file

app/code/VendorName/ModuleName/view/Adminhtml/templates

appdashboard.phtml

<h1>hello I am From phtml file<?php echo $block->sayHello(); ?></h1>


Now filanlly create a Layout file

layout file name must be as this formate and all latters are in lower case.

[admin_route_name]_[admin_controller_name]_[Controller_filename].xml

app/code/VendorName/ModuleName/view/Adminhtml/layout

[admin_route_name]_appdashboard_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="VendorName/ModuleName/Block/Adminhtml/Dashboard" template="VendorName_ModuleName::appdashboard.phtml"/>
        </referenceContainer>
    </body>
</page>

Craete an Menu for acceess this layout

app/code/VendorName/ModuleName/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="VendorName_ModuleName::content_elements" title="Pages Menu" module="VendorName_ModuleName" sortOrder="20"  resource="VendorName_ModuleName::content_elements" />

 <add id="VendorName_ModuleName::showpage" title="show Page" module="VendorName_ModuleName" sortOrder="40" parent="VendorName_ModuleName::content_elements" action="[admin_route_name]/appdashboard/index" resource="VendorName_ModuleName::showpage"/>

    </menu>
</config>

Now run Cammand in Terminal

php bin/magento s:up
php bin/magento c:f
php bin/magento c:c

I Hope This helps you

| improve this answer | |
  • @hari let me know the above solution is working or not for you? – Msquare Jan 26 at 4:42

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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