Skip to content

Latest commit

 

History

History
182 lines (142 loc) · 5.45 KB

6.Admin-url-using-route-acl.md

File metadata and controls

182 lines (142 loc) · 5.45 KB

Base required files:

Admin Url Format AdmintURL/{frontName}/{ControllerFolderInSmalllaetter}/{ActionFileNameINSameLetter}/...

  • app/code/{Vendor}/{ModuleName}/etc/adminhtml/routes.xml (Define route id and frontName)
  • app/code/{Vendor}/{ModuleName}/etc/acl.xml (For ACL)
  • app/code/{Vendor}/{ModuleName}/Controller/Adminhtml/{ControllerFolderName}/{ActionFileName}.php(For URL)

--Some Source--

routes.xml

Under etc/adminhtml create the file routes.xml. The contents of this XML file tells Magento to route requests that use the frontName exampleadminnewpage to this module

Location should be: app/code/{Vendor}/{ModuleName}/etc/adminhtml

Format

<?xml version="1.0" encoding="UTF-8"?>
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
        xsi:noNamespaceSchemaLocation="urn:magento:framework:App/etc/routes.xsd">
    <router id="admin">
        <route id="{RouteId}" frontName="{frontName}">
            <module name="{Vendor}_{ModuleName}"/>
        </route>
    </router>
</config>

acl.xml

Location should be: app/code/{Vendor}/{ModuleName}/etc/

Format

<?xml version="1.0" encoding="UTF-8"?>
<!-- Create Acl for Admin Permission -->
<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"> <!-- Indicated you at admin section -->
                    <resource id="{{Vendor}_{ModuleName}}::{UserDefineId1}" title="{UserDefineIdLabel1}" sortOrder="{sortOrder}">
                       <!-- Create an  ACL resource id for Edit Example Url for this module -->
                       <resource id="{Vendor}_{ModuleName}::{UserDefineId2}" title="{UserDefineIdLabel2}" sortOrder="{sortOrder"/>
                       <!-- Create an  ACL resource id for Delete Action Url for this module -->
                        <resource id="{Vendor}_{ModuleName}::{UserDefineId3}" title="{UserDefineIdLabel3}" sortOrder="{sortOrder"/>
                   </resource>                    
               </resource>
       </resources>
   </acl>
</config>

Example

<?xml version="1.0" encoding="UTF-8"?>
<!-- Create Acl for Admin Permission -->
<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"> <!-- Indicated you at admin section -->
                     <resource id="StackExchange_Example::example_listing" title="Manage Example" sortOrder="500">
                        <!-- Create an  ACL resource id for Edit Example Url for this module -->
                        <resource id="StackExchange_Example::example_edit" title="Edit Example" sortOrder="10"/>
                        <!-- Create an  ACL resource id for Delete Action Url for this module -->
                         <resource id="StackExchange_Example::example_delete" title="Delete Example" sortOrder="20"/>
                    </resource>                    
                </resource>
        </resources>
    </acl>
</config>

ActionFileName.php

Location should be: app/code/{Vendor}/{ModuleName}/Controller/Adminhtml/{ControllerFolderName}/

Format

<?php


namespace {Vendor}\{ModuleName\Controller\Adminhtml\{ControllerFolderName};

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

class {ActionFileName} extends  \Magento\Backend\App\Action
{
   /**
    * @url adminUrl/frontName/ControllerFolder/ActionfileName
    * @example adminUrl/exampleadmin/example/listing
    */

   /**
    * @var \Magento\Framework\View\Result\PageFactory
    */
   private $resultPageFactory;

   public function __construct(
       Context $context,
       PageFactory $resultPageFactory   
    ) {
       parent::__construct($context);
       $this->resultPageFactory = $resultPageFactory;
   }
   public function execute() 
   {
       return $this->resultPageFactory->create();
   }

}

Example:

<?php


namespace StackExchange\Example\Controller\Adminhtml\Example;

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

class Listing extends  \Magento\Backend\App\Action
{
    /**
     * @url adminUrl/frontName/ControllerFolder/ActionfileName
     * @example adminUrl/exampleadmin/example/listing
     */

    /**
     * @var \Magento\Framework\View\Result\PageFactory
     */
    private $resultPageFactory;

    public function __construct(
        Context $context,
        PageFactory $resultPageFactory   
     ) {
        parent::__construct($context);
        $this->resultPageFactory = $resultPageFactory;
    }
    public function execute() 
    {
        return $this->resultPageFactory->create();
    }

}

Structure

{Vendor}
-- {ModuleName}
    |
    |-- Controller
    |   |-- Adminhtml
    |       |-- {ControllerFolderName}
    |           -- {ActionFileName}.php
    |-- etc
    |   |-- adminhtml
    |   |   -- routes.xml
    |   -- module.xml
    |   -- acl.xml
    |-- composer.json
    -- registration.php