Magento – Add an admin with basic custom form for Magento2

adminhtmlmagento2

I am trying to create an admin page for my extension.
I managed to add a new button to the side menu just below the System icon by adding the following menu.xml under etc/adminhtml.

<?xml version="1.0"?>
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="../../../../Magento/Backend/etc/menu.xsd">
    <menu>
        <add id="Test_Admin::adminpage" title="Test" module="Test_Admin" sortOrder="300" action="test/index" resource="Test_Admin::adminpage"/>
    </menu>
</config>

But I can't figure out how to route this button to a new page, this page should contain a form among other html.

In addition what is the way to create db-configs for my extension for the user to edit.

Best Answer

So firstly you can add a link to your menu and specify the module and action. For example your module could have the following xml. app/code/Stack/Example/etc/adminhtml/menu.xml

<?xml version="1.0"?>
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
        xsi:noNamespaceSchemaLocation="../../../../Magento/Backend/etc/menu.xsd">
    <menu>
        <add id="Stack_Example::your_content" title="Your Content" module="Stack_Example" sortOrder="50"
             parent="Magento_Backend::content" resource="Stack_Example::your_content"/>
        <add id="Stack_Example::content" title="Content" module="Stack_Example" sortOrder="0"
             parent="Stack_Example::your_content" action="stack_example/content/info" resource="Stack_Example::content"/>
    </menu>
</config>

Now the action is split as follows:

stack_example/content/info
router/folder/controller

So in our case we need the router stack_example defined in the 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="stack_example" frontName="stack_example">
            <module name="Stack_Example" before="Magento_Backend"/>
        </route>
    </router>
</config>

Then the controller will be under Stack/Example/Controller/Adminhtml/Content/Info.php and should look as follows.

<?php
namespace Stack\Example\Controller\Adminhtml\Content;

use Magento\Backend\App\Action\Context;
use Magento\Framework\View\Result\PageFactory;

class Info extends \Magento\Backend\App\Action
{
    const ADMIN_RESOURCE = 'Stack_Example::content';

    /**
     * @var PageFactory
     */
    protected $resultPageFactory;

    /**
     * @param Context $context
     * @param PageFactory $resultPageFactory
     */
    public function __construct(
        Context $context,
        PageFactory $resultPageFactory
    ) {
        parent::__construct($context);
        $this->resultPageFactory = $resultPageFactory;
    }

    /**
     * Update the breadcrumb and extra information
     *
     * @return \Magento\Backend\Model\View\Result\Page
     */
    public function execute()
    {
        /** @var \Magento\Backend\Model\View\Result\Page $resultPage */
        $resultPage = $this->resultPageFactory->create();
        $resultPage->addBreadcrumb(__('Stack Example'), __('Content'));
        $resultPage->getConfig()->getTitle()->prepend(__('Content'));

        return $resultPage;
    }
}