Magento 2 – Custom Module Admin Controller Error

customextensionsmagento2module

I have created the custom module as per the below tutorial:

http://www.mage-world.com/blog/grid-and-form-in-magento-2-admin-panel-part-1.html

And after I am getting the below error:

Fatal error: Class Tutorial\SimpleNews\Controller\Adminhtml\News contains
1 abstract method and must therefore be declared abstract or implement
the remaining methods (Magento\Framework\App\ActionInterface::execute)
in
C:\xampp\htdocs\magento3\app\code\Tutorial\SimpleNews\Controller\Adminhtml\News.php
on line 12

After that I have done some google and found the solution for the execute() function in below answer:

Error in custom Admin menu controller – Magento 2

According to the answer I have changed the class name from class to abstract class.

After that I am getting the below error:

Boolean value is expected, supported values: array (
  0 => true,
  1 => 1,
  2 => 'true',
  3 => '1',
  4 => false,
  5 => 0,
  6 => 'false',
  7 => '0',
)

Please see below is the Tutorial\SimpleNews\Controller\Adminhtml\News.php file code:

<?php

namespace Tutorial\SimpleNews\Controller\Adminhtml;

use Magento\Backend\App\Action;
use Magento\Backend\App\Action\Context;
use Magento\Framework\Registry;
use Magento\Framework\View\Result\PageFactory;
use Tutorial\SimpleNews\Model\NewsFactory;

abstract class News extends Action
{
    /**
     * Authorization level
     *
     * @see _isAllowed()
     */
     const ADMIN_RESOURCE = 'News_LatestNews::‌​manage_news';

     /**
     * Core registry
     *
     * @var \Magento\Framework\Registry
     */
    protected $_coreRegistry;

    /**
     * Result page factory
     *
     * @var \Magento\Framework\View\Result\PageFactory
     */
    protected $_resultPageFactory;

    /**
     * News model factory
     *
     * @var \Tutorial\SimpleNews\Model\NewsFactory
     */
    protected $_newsFactory;

    /**
     * @param Context $context
     * @param Registry $coreRegistry
     * @param PageFactory $resultPageFactory
     * @param NewsFactory $newsFactory
     */
    public function __construct(
        Context $context,
        Registry $coreRegistry,
        PageFactory $resultPageFactory,
        NewsFactory $newsFactory
    ) {
       parent::__construct($context);
        $this->_coreRegistry = $coreRegistry;
        $this->_resultPageFactory = $resultPageFactory;
        $this->_newsFactory = $newsFactory;
    }


}

Tutorial\SimpleNews\Controller\Adminhtml\News\Index.php file code:

<?php

namespace Tutorial\SimpleNews\Controller\Adminhtml\News;

use Tutorial\SimpleNews\Controller\Adminhtml\News;

class Index extends News
{

    /**
    * @return void
    */
    public function execute()
    {
      if ($this->getRequest()->getQuery('ajax')) {
            $this->_forward('grid');
            return;
        }

        /** @var \Magento\Backend\Model\View\Result\Page $resultPage */
        $resultPage = $this->_resultPageFactory->create();
        $resultPage->setActiveMenu('Tutorial_SimpleNews::main_menu');
        $resultPage->getConfig()->getTitle()->prepend(__('Simple News'));

        return $resultPage;
    }
}

I have done the var_dump($this->_authorization->isAllowed('News_LatestNews::‌​manage_news')); also it's output it bool(true).

Anybody, can help me how I can trace this issue?

Best Answer

I suppose that you already have your module Tutorial_SimpleNews created and enabled in your store.

I've run this code on M2.1.3.

1/ In app/code/Tutorial/SimpleNews/etc/adminhtml/ folder create an routes.xml file with this code:

<?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 frontName="news" id="news">
            <module name="Tutorial_SimpleNews" />
        </route>
    </router>
</config>

2/ In app/code/Tutorial/SimpleNews/etc/ folder create an acl.xml file with this code:

<?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">
                <resource id="Tutorial_SimpleNews::manage_news" title="New" sortOrder="10" />
            </resource>
        </resources>
    </acl>
</config>

3/ In app/code/Tutorial/SimpleNews/etc/adminhtml/ folder create an menu.xml file with this code:

<?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="Tutorial_SimpleNews::manage_news"
             title="News"
             module="Tutorial_SimpleNews"
             sortOrder="1"
             resource="Tutorial_SimpleNews::manage_news"
             action="news/news"
        />
    </menu>
</config>

4/ In app/code/Tutorial/SimpleNews/Controller/Adminhtml/ folder create News.php file with this code:

<?php

namespace Tutorial\SimpleNews\Controller\Adminhtml;

use Magento\Backend\App\Action;
use Magento\Backend\App\Action\Context;
use Magento\Framework\Registry;
use Magento\Framework\View\Result\PageFactory;

abstract class News extends Action
{
    /**
     * Authorization level
     *
     * @see _isAllowed()
     */
     const ADMIN_RESOURCE = 'Tutorial_SimpleNews::manage_news';

     /**
     * Core registry
     *
     * @var \Magento\Framework\Registry
     */
    protected $_coreRegistry;

    /**
     * Result page factory
     *
     * @var \Magento\Framework\View\Result\PageFactory
     */
    protected $_resultPageFactory;

    /**
     * @param Context $context
     * @param Registry $coreRegistry
     * @param PageFactory $resultPageFactory
     */
    public function __construct(
        Context $context,
        Registry $coreRegistry,
        PageFactory $resultPageFactory
    ) {
       parent::__construct($context);
        $this->_coreRegistry = $coreRegistry;
        $this->_resultPageFactory = $resultPageFactory;
    }
}

5/ In app/code/Tutorial/SimpleNews/Controller/Adminhtml/News/ folder create Index.php file with this code:

<?php

namespace Tutorial\SimpleNews\Controller\Adminhtml\News;

use Tutorial\SimpleNews\Controller\Adminhtml\News;

class Index extends News
{
    /**
    * @var \Magento\Backend\Model\View\Result\Page
    */
    public function execute()
    {
      if ($this->getRequest()->getQuery('ajax')) {
            $this->_forward('grid');
            return;
        }

        /** @var \Magento\Backend\Model\View\Result\Page $resultPage */
        $resultPage = $this->_resultPageFactory->create();
        $resultPage->setActiveMenu('Tutorial_SimpleNews::main_menu');
        $resultPage->getConfig()->getTitle()->prepend(__('Simple News'));

        return $resultPage;
    }
}

6/ Make sure the role your are accessing your admin let you access this action

7/ In your Magento root directory, run commands:

rm -rf var/cache/*

php bin/magento setup:di:compile

8/ Open your browser and access your Magento admin (logged out if you were logged in before), you will see a new menu entry on the left with a link to your controller called "News".