0

I created a custom menu but got this error.

Fatal error: Class News\LatestNews\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\News\LatestNews\Controller\Adminhtml\News.php on line 12

these are the codes

News\LatestNews\Controller\Adminhtml\News.php

<?php 
namespace News\LatestNews\Controller\Adminhtml;

use Magento\Backend\App\Action;
use Magento\Backend\App\Action\Context;
use Magento\Framework\Registry;
use Magento\Framework\View\Result\PageFactory;
use News\LatestNews\Model\NewsFactory;

abstract class News extends Action
{
/**
 * Core registry
 *
 * @var \Magento\Framework\Registry
 */
protected $_coreRegistry;

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

/**
 * News model factory
 *
 * @var \News\LatestNews\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;
}

/**
 * News access rights checking
 *
 * @return bool
 */
    protected function _isAllowed()
{
    return         $this->_authorization->isAllowed('News_LatestNews::manage_news');
}
}

News\LatestNews\Controller\Adminhtml\News\Index.php

<?php

namespace News\LatestNews\Controller\Adminhtml\News;

use News\LatestNews\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('News_LatestNews::main_menu');
    $resultPage->getConfig()->getTitle()->prepend(__('Simple News'));

    return $resultPage;
   }
}

News\LatestNews\Block\Adminhtml\News\News.php

<?php

namespace News\LatestNews\Block\Adminhtml;

use Magento\Backend\Block\Widget\Grid\Container;

class News extends Container
{
   /**
     * Constructor
     *
     * @return void
     */
   protected function _construct()
      {
        $this->_controller = 'adminhtml_news';
        $this->_blockGroup = 'News_LatestNews';
        $this->_headerText = __('Manage News');
        $this->_addButtonLabel = __('Add News');
        parent::_construct();
      }
   }

News\LatestNews\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="News_LatestNews::main_menu" title="Simple News" 
            module="News_LatestNews" sortOrder="20" 
            resource="News_LatestNews::simplenews" />
        <add id="News_LatestNews::add_news" title="Add News" 
            module="News_LatestNews" sortOrder="1" parent="News_LatestNews::main_menu" 
            action="simplenews/news/new" resource="News_LatestNews::manage_news" />
        <add id="News_LatestNews::manage_news" title="Manage News" 
            module="News_LatestNews" sortOrder="2" parent="News_LatestNews::main_menu" 
            action="simplenews/news/index" resource="News_LatestNews::manage_news" />
        <add id="News_LatestNews::configuration" title="Configurations" 
            module="News_LatestNews" sortOrder="3" parent="News_LatestNews::main_menu" 
            action="adminhtml/system_config/edit/section/simplenews" 
            resource="News_LatestNews::configuration" />
    </menu>
</config>

News\LatestNews\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="simplenews" frontName="simplenews">
            <!-- <module name="Tutorial_SimpleNews" /> -->
            <module name="News_LatestNews" />
        </route>
    </router>
</config>

News\LatestNews\etc\acl.xml

<?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="News_LatestNews::simplenews" title="Simple News" 
                sortOrder="100">
                <resource id="News_LatestNews::add_news" title="Add News" 
                    sortOrder="1" />
                <resource id="News_LatestNews::manage_news" title="Manage News" 
                    sortOrder="2" />
                <resource id="News_LatestNews::configuration" title="Configurations" 
                    sortOrder="3" />
            </resource> 

            <resource id="Magento_Backend::stores">
                <resource id="Magento_Backend::stores_settings">
                    <resource id="Magento_Config::config">
                        <resource id="News_LatestNews::config" title="News Configuration" sortOrder="50" />
                    </resource>
                </resource>
            </resource>
      </resource>       
    </resources>
</acl>
</config>

News\LatestNews\etc\module.xml

<?xml version="1.0"?>

<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:Module/etc/module.xsd">
    <module name="News_LatestNews" setup_version="1.0.2" active="true">
    </module>
</config>

Edit:

1 exception(s):
    Exception #0 (InvalidArgumentException): Boolean value is expected, supported values: array (
      0 => true,
      1 => 1,
      2 => 'true',
      3 => '1',
      4 => false,
      5 => 0,
      6 => 'false',
      7 => '0',
    )
  • php bin/magento setup:di:compile run & check – Ankit Shah Feb 17 '17 at 6:01
  • delete \var\generation folder and check. – Anand Ontigeri Feb 17 '17 at 6:05
3

The error is self-explained. \News\LatestNews\Controller\Adminhtml\News extends from abstract class Magento\Backend\App\Action which implements Magento\Framework\App\ActionInterface::execute(). So, in your custom controller class need to have execute() method.

Or, \News\LatestNews\Controller\Adminhtml\News is an abstract class.

  • in News\LatestNews\Controller\Adminhtml\News\Index.php there is an execute method which is extended in News\LatestNews\Controller\Adminhtml\News , is it wrong? – enjames Feb 17 '17 at 7:00
  • @enjames News\LatestNews\Controller\Adminhtml\News should be an abstract class. So, you don't need to declare the execute method in this class. Your index controller will extend from it and declare execute method . – Khoa TruongDinh Feb 17 '17 at 7:15
  • O got a new error 1 exception(s): Exception #0 (InvalidArgumentException): Boolean value is expected, supported values: array ( 0 => true, 1 => 1, 2 => 'true', 3 => '1', 4 => false, 5 => 0, 6 => 'false', 7 => '0', ) Exception #0 (InvalidArgumentException): Boolean value is expected, supported values: array ( 0 => true, 1 => 1, 2 => 'true', 3 => '1', 4 => false, 5 => 0, 6 => 'false', 7 => '0', ) i just add abstract abstract class News extends Action, – enjames Feb 17 '17 at 8:27
  • @enjames you can update your question with the new code lines? – Khoa TruongDinh Feb 17 '17 at 8:39
  • You tried to var_dump() what? You tried to use var_dump and then show the new error? – Khoa TruongDinh Feb 17 '17 at 9:03

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.