0

I'm learning how to create new pages for my custom extension in magento2 admin.

However when i go to that admin page it's blank.

I've got a controller:

<?php

namespace ZET3K\Tracker\Controller\Adminhtml\Config;

class Index extends \Magento\Backend\App\Action
{
    /**
     * @var \Magento\Framework\View\Result\PageFactory
     */
    public $resultPageFactory;

    /**
     * Index constructor.
     *
     * @param \Magento\Backend\App\Action\Context $context
     * @param \Magento\Framework\View\Result\PageFactory $resultPageFactory
     */
    public function __construct(
        \Magento\Backend\App\Action\Context $context,
        \Magento\Framework\View\Result\PageFactory $resultPageFactory
    ) {
        parent::__construct($context);
        $this->resultPageFactory = $resultPageFactory;
    }

    /**
     * @return bool
     */
    protected function _isAllowed()
    {
        return true;
    }

    /**
     * @return \Magento\Backend\Model\View\Result\Page
     */
    public function execute()
    {
        //Call page factory to render layout and page content
        /** @var \Magento\Backend\Model\View\Result\Page $resultPage */
        $resultPage = $this->resultPageFactory->create();
        //Set the menu which will be active for this page
        $resultPage->setActiveMenu('ZET3K_Tracker::config');
        //Set the header title of grid
        $resultPage->getConfig()->getTitle()->prepend(__('Tracker Configuration'));

        return $resultPage;
    }
}

And a layout:

<?xml version="1.0"?>
<page xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" layout="admin-1column"
      xsi:noNamespaceSchemaLocation="urn:magento:framework:View/Layout/etc/page_configuration.xsd">
    <body>
        <referenceContainer name="content">
            <block class="ZET3K\Tracker\Block\Adminhtml\Config" template="ZET3K_Tracker::config.phtml" />
        </referenceContainer>
    </body>
</page>

And a block:

<?php

namespace ZET3K\Tracker\Block\Adminhtml;

class Config extends \Magento\Framework\View\Element\Template
{
    public function sayHello()
    {
        return 'heyyyy';
    }
}

In the template i try $block->sayHello() but the page is empty. Only has a title. Any advice? Thanks

0

Change execute method:

 public function execute()
    {
        //Call page factory to render layout and page content
        /** @var \Magento\Backend\Model\View\Result\Page $resultPage */
        $resultPage = $this->resultPageFactory->create();
        //Set the menu which will be active for this page
        $resultPage->setActiveMenu('ZET3K_Tracker::config');
        //Set the header title of grid
        $resultPage->getConfig()->getTitle()->prepend(__('Tracker Configuration'));

        $this->_view->renderLayout();
    }
0

I found the where the issue is. I had 'template' instead of 'templates' as my folder name.

0

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.