2

I'm getting error while compiling the Magento 2 admin module. The error which I'm getting is

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 /Applications/XAMPP/xamppfiles/htdocs/MagentoTest/app/code/Tutorial/SimpleNews/Controller/Adminhtml/News.php on line 60

Here is my 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;

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 \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;
    }

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

Yes.. you need to add the execute method in the controller action..eg:-

public function execute() {

    $this->resultPage = $this->resultPageFactory->create();
    $this->resultPage->setActiveMenu('Tutorial_SimpleNews::manage');
    return $this->resultPage;       
}
6
  • Still I'm getting the same Error.. Nov 25 '16 at 6:11
  • Actually the correct way of writing a controller action in M2 is we have to create a folder inside Adminhtml and then place the controller in that.Try to do in that way. It seems your controller action file is correct. Dont forget to clear generation.
    – Rahul
    Nov 25 '16 at 6:59
  • Sorry I have wrongly updated the code.. Nov 25 '16 at 7:01
  • I have updated the code. please have a look Nov 25 '16 at 7:02
  • You must add the execute method for the controller and then you can add your remaining methods too.
    – Rahul
    Nov 25 '16 at 7:05
-2

Default Abstract clss is required.

class News extends Action

change to

abstract class News extends Action

execute method not required.

So your file at app\code\Tutorial\SimpleNews\Controller\Adminhtml\New.php

<?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
{


   /**
     * 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;
    }

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

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.