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 anroutes.xml
file with this code:2/ In
app/code/Tutorial/SimpleNews/etc/
folder create anacl.xml
file with this code:3/ In
app/code/Tutorial/SimpleNews/etc/adminhtml/
folder create anmenu.xml
file with this code:4/ In
app/code/Tutorial/SimpleNews/Controller/Adminhtml/
folder createNews.php
file with this code:5/ In
app/code/Tutorial/SimpleNews/Controller/Adminhtml/News/
folder createIndex.php
file with this code: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".