As Magento compile the classes and put them in var/generation folder, So it will always be a better practice to delete var/generation and var/cache folder, whenever you created a new class or change any parameters of constructor of any class.
If you don't want to delete whole folder then try to delete files from the same class path from where you are getting error in generation folder, hopefully you will be able to debug the problem.
Another solution is to compile your code by setup:di:compile command but in developing phase it become cumbersome for developer to run this time taking command to just get a simple solution :) :)
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".
Best Answer
Yes.. you need to add the execute method in the controller action..eg:-