0

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?

0

3 Answers 3

1

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".

13
  • I have added the const as per your answer but still getting same error. Feb 28, 2017 at 12:04
  • Then can you remove protected method _isAllowed in abastract class News please? Can you also show the code of your Index Class in your question please? Feb 28, 2017 at 12:22
  • I have removed the protected method _isAllowed method and added the Index Class code. Feb 28, 2017 at 12:28
  • @mageDev0688 check my updated answer please. Feb 28, 2017 at 12:43
  • I have gone through you updated answer, but I am getting same error. Thanks for your help!!! Feb 28, 2017 at 13:25
0

That tutorial is 1.5 years old. Things have changed since then.

Make your class News\LatestNews\Controller\Adminhtml\News as abstract and you should be ok.

You need to do that because all classes that extend the abstract controller need to have a method called execute that is the actual method that handles the request.
Since you are never instantiating your class you can make it abstract and the classes that extend your class need to contain the execute method.

3
  • I have added the excute() function. Please check the updated Controller file. Feb 28, 2017 at 11:44
  • execute() method should be in your Index class not in Abstract. Feb 28, 2017 at 11:47
  • I have added the execute() function in the index class. Feb 28, 2017 at 12:10
0

Try to check and change your XML on adminhtnl/layout -> simplenews_news_grid_block.xml

<argument name="dataSource" xsi:type="object">
      Tutorial\SimpleNews\Model\Resource\News\Collection
</argument>
<argument name="default_sort" xsi:type="string">id</argument>
<argument name="default_dir" xsi:type="string">desc</argument>
<argument name="save_parameters_in_session" xsi:type="boolean">           true</argument>

to

<argument name="dataSource" xsi:type="object">Tutorial\SimpleNews\Model\Resource\News\Collection</argument>
<argument name="default_sort" xsi:type="string">id</argument>
<argument name="default_dir" xsi:type="string">desc</argument>
<argument name="save_parameters_in_session" xsi:type="boolean">true</argument>

no space before and after value >value <

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

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