Magento 2 Training : Unit 3 – Lesson E

Backend controller declaration

In the last Unit, we learnt how to create a frontend controller, now let’s create an admin controller.

Create the file :
app/code/Maxime/Jobs/etc/adminhtml/routes.xml

You can put this content inside :

<?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 id="jobs" frontName="jobs">
            <module name="Maxime_Jobs" before="Magento_Backend" />
        </route>
    </router>
</config>

The composition of the node route is :
– id : unique identifier for your router
– frontName : the string which be used on the admin URL

Then, there is the module node with these attributes:
– name : module name to call
– before : your module have to be called before the module defined inside
– after : you module have to be called after the module defined inside

“before” et “after” fields are NOT required

Admin controller creation

Remember the “menu.xml” file of the last part :

<?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="Maxime_Jobs::job_head" title="Jobs" module="Maxime_Jobs" sortOrder="100" resource="Maxime_Jobs::job_head" />
        <add id="Maxime_Jobs::department" title="Departments" module="Maxime_Jobs" sortOrder="10" parent="Maxime_Jobs::job_head" action="jobs/department" resource="Maxime_Jobs::job" />
        <add id="Maxime_Jobs::job" title="Jobs" module="Maxime_Jobs" sortOrder="20" parent="Maxime_Jobs::job_head" action="jobs/job" resource="Maxime_Jobs::job" />
    </menu>
</config>

Look at the lines 5 and 6, we have these values :
– jobs/department
– jobs/job

If you remember how magento translate these action, you can guess the controllers path files !

The real pathes for Magento are :
– jobs/department/index
– jobs/job/index

So we can create these 2 controllers :
– app/code/Maxime/Jobs/Controller/Adminhtml/Department/Index.php
– app/code/Maxime/Jobs/Controller/Adminhtml/Job/Index.php

The difference with frontend controller is the folder “Adminhtml”, in order to separate them.

So, create the first controller :

app/code/Maxime/Jobs/Controller/Adminhtml/Department/Index.php

With the following content :

<?php
namespace Maxime\Jobs\Controller\Adminhtml\Department;

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

class Index extends Action
{
    const ADMIN_RESOURCE = 'Maxime_Jobs::department';

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

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

    /**
     * Index action
     *
     * @return \Magento\Backend\Model\View\Result\Page
     */
    public function execute()
    {
        /** @var \Magento\Backend\Model\View\Result\Page $resultPage */
        $resultPage = $this->resultPageFactory->create();
        $resultPage->setActiveMenu('Maxime_Jobs::department');
        $resultPage->addBreadcrumb(__('Jobs'), __('Jobs'));
        $resultPage->addBreadcrumb(__('Manage Departments'), __('Manage Departments'));
        $resultPage->getConfig()->getTitle()->prepend(__('Department'));

        return $resultPage;
    }
}

I will explain you the content point by point :

– The ADMIN_RESOURCE constant is very important. It’s define the permission we define on the ACL :

                        <resource id="Maxime_Jobs::department" title="Departments" sortOrder="10">
                            <resource id="Maxime_Jobs::department_save" title="Save Department" sortOrder="10" />
                            <resource id="Maxime_Jobs::department_delete" title="Delete Department" sortOrder="20" />
                        </resource>

We put the id of the first line on this contant

– The “resultPageFactory” attribute and the construct method are required. It’s dependancy injection, without this part you’ll have the following error :
Notice: Undefined property: Maxime\Jobs\Controller\Adminhtml\Job\Index\Interceptor::$resultPageFactory

– On the “execute” method, we define some information:
setActive menu allow you to choose the current menu, which is declared on menu.xml file.

activemenu

– addBreadcrumb methods allow you to add some items to the breadcrumb. First parameter is element label, second is the title.
A third parameters is available and it’s the link of the element.

public function addBreadcrumb($label, $title, $link = null)

The breadcrumb doesn’t seem visible on the admin part, but natives modules call this methods, so I did the same.

– preprend modify the title of the page : “Department / Jobs / Magento Admin”

It’s the same thing for the Job admin controller :

app/code/Maxime/Jobs/Controller/Adminhtml/Job/Index.php

With the content :

<?php
namespace Maxime\Jobs\Controller\Adminhtml\Job;

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

class Index extends Action
{
    const ADMIN_RESOURCE = 'Maxime_Jobs::job';

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

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

    /**
     * Index action
     *
     * @return \Magento\Backend\Model\View\Result\Page
     */
    public function execute()
    {
        /** @var \Magento\Backend\Model\View\Result\Page $resultPage */
        $resultPage = $this->resultPageFactory->create();
        $resultPage->setActiveMenu('Maxime_Jobs::job');
        $resultPage->addBreadcrumb(__('Jobs'), __('Jobs'));
        $resultPage->addBreadcrumb(__('Manage Jobs'), __('Manage Jobs'));
        $resultPage->getConfig()->getTitle()->prepend(__('Job'));

        return $resultPage;
    }
}

Now, if you click on the menu links, you will access to the good page, but they are empty 🙁

On the next lessons, we will add a grid with a list of our elements, add sort and filters etc… Like the orders admin page.
Have a little break, because the next part will be one of the hardest of the training.

Continue training
Return to previous lesson
Admin controller creation with Magento 2
Tagged on:         

One thought on “Admin controller creation with Magento 2

  • 02/15/2017 at 10:28
    Permalink

    Hi,

    There’s something that doesn’t works because i’m always redirect to home of the backend.
    Have you some place where i can download your module?

    thanks, good job

    Reply

Leave a Reply

Your email address will not be published. Required fields are marked *

We use cookies to ensure that we give you the best experience on our website.
Ok