Magento Expert Forum - Improve your Magento experience

Results 1 to 2 of 2

How to Create Controller in Magento 2

  1. #1
    Junior Member
    Join Date
    May 2017
    Location
    1A Sportyvna square, Business center Regus Gulliver, 17 floor Kiev, Ukraine 01601
    Posts
    112
    Thanks
    0
    Thanked 3 Times in 3 Posts

    Default How to Create Controller in Magento 2


  2. #2
    Junior Member
    Join Date
    Sep 2018
    Posts
    57
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default

    To create a custom controller, we need to go through these steps:

    Step 1: Create routers.xml file
    Create routes.xml in etc/frontend for frontend controller or etc/adminhtml folder for admin controller.
    Example:
    etc/frontend/routes.xml – routes configuration for frontend; for demonstration we will match “magentofronttest” as frontname (part of url after domain name – for example: www.magentotest.net/magentofronttest/)

    <span class="sc3"><span class="re1">&lt;config</span> <span class="re0">xmlns:xsi</span>=<span class="st0">"http://www.w3.org/2001/XMLSchema-instance"</span> <span class="re0">xsi:noNamespaceSchemaLocation</span>=<span class="st0">"../../../../../../lib/internal/Magento/Framework/App/etc/routes.xsd"</span><span class="re2">&gt;</span></span>
    <span class="sc3"><span class="re1">&lt;router</span> <span class="re0">id</span>=<span class="st0">"standard"</span><span class="re2">&gt;</span></span>
    <span class="sc3"><span class="re1">&lt;route</span> <span class="re0">id</span>=<span class="st0">"magentotestfrontend"</span> <span class="re0">frontName</span>=<span class="st0">"magentofronttest"</span><span class="re2">&gt;</span></span>
    <span class="sc3"><span class="re1">&lt;module</span> <span class="re0">name</span>=<span class="st0">"Magento_CustomControllers"</span> <span class="re2">/&gt;</span></span>
    <span class="sc3"><span class="re1">&lt;/route<span class="re2">&gt;</span></span></span>
    <span class="sc3"><span class="re1">&lt;/router<span class="re2">&gt;</span></span></span>
    <span class="sc3"><span class="re1">&lt;/config<span class="re2">&gt;</span></span></span>


    Step 2: Custom configuration of controller

    Add your custom configuration for controller in routes.xml, for example:
    – router: id – standard(frontend)/admin
    – route: id – your unique route id
    – route: frontName – unique name in url, this is first part of url in base router (http://www.yourwebsite.com/frontName...h/actionclass/)
    frontName – set in routes.xml configuration, and has unique value which will be matched by router
    action path – folder name inside Controller folder, default is index
    action class – our action class which we call Controller, default is index
    – module name – your module name
    Example:
    etc/adminhtml/routes.xml – routes configuration for admin, for demonstration we will match “magentoadmintest” as frontname (part of url after /admin/)



    <span class="sc3"><span class="re1">&lt;config</span> <span class="re0">xmlns:xsi</span>=<span class="st0">"http://www.w3.org/2001/XMLSchema-instance"</span> <span class="re0">xsi:noNamespaceSchemaLocation</span>=<span class="st0">"../../../../../../lib/internal/Magento/Framework/App/etc/routes.xsd"</span><span class="re2">&gt;</span></span>
    <span class="sc3"><span class="re1">&lt;router</span> <span class="re0">id</span>=<span class="st0">"admin"</span><span class="re2">&gt;</span></span>
    <span class="sc3"><span class="re1">&lt;route</span> <span class="re0">id</span>=<span class="st0">"magentoadmintest"</span> <span class="re0">frontName</span>=<span class="st0">"magentoadmintest"</span><span class="re2">&gt;</span></span>
    <span class="sc3"><span class="re1">&lt;module</span> <span class="re0">name</span>=<span class="st0">"Magento_CustomControllers"</span> <span class="re0">before</span>=<span class="st0">"Magento_Backend"</span> <span class="re2">/&gt;</span></span>
    <span class="sc3"><span class="re1">&lt;/route<span class="re2">&gt;</span></span></span>
    <span class="sc3"><span class="re1">&lt;/router<span class="re2">&gt;</span></span></span>
    <span class="sc3"><span class="re1">&lt;/config<span class="re2">&gt;</span></span></span>


    Step 3: Create action controller
    Create your action class following the url structure above:
    Controller/Actionpath/Actionclass.php
    Now we will create action classes for both of the controllers configuration. On frontend let’s create action path and action class for url siteurl/magentoronttest/demonstration/sayhello/. You can see that we need Demonstration folder (this is called action path) inside of our Controller folder, and inside that folder we need Sayhello.php controller action class:

    Controller/Demonstration/Sayhello.php:



    <span class="kw2">&lt;?php
    </span>
    <span class="kw2">namespace</span> Magento\CustomControllers\Controller\Demonstration <span class="sy0">;</span>

    <span class="kw2">class</span> Sayhello <span class="kw2">extends</span> \Magento\Framework\App\Action\Action
    <span class="br0">{</span>
    <span class="co4">/**
    * say hello text
    */</span>
    <span class="kw2">public</span> <span class="kw2">function</span> execute<span class="br0">(</span><span class="br0">)</span>
    <span class="br0">{</span>
    <span class="kw3">die</span><span class="br0">(</span><span class="st0">"Hello - Magento<span class="es1">\\</span>CustomControllers<span class="es1">\\</span>Controller<span class="es1">\\</span>Demonstration<span class="es1">\\</span>Sayhello - execute() method"</span><span class="br0">)</span><span class="sy0">;</span>
    <span class="br0">}</span>
    <span class="br0">}</span>


    For admin controller let’s match url siteurl/admin/magentoadmintest/demonstration/sayadmin/. For that we need Demonstration folder in Controller/Adminhtml, and inside that action class we need to create Sayadmin.php

    Controller/Adminhtml/Demonstration/Sayadmin.php



    <span class="kw2">&lt;?php</span>

    <span class="kw2">namespace</span> Magento\CustomControllers\Controller\Adminhtml\Dem onstration<span class="sy0">;</span>

    <span class="kw2">class</span> Sayadmin <span class="kw2">extends</span> \Magento\Backend\App\Action
    <span class="br0">{</span>
    <span class="co4">/**
    * say admin text
    */</span>
    <span class="kw2">public</span> <span class="kw2">function</span> execute<span class="br0">(</span><span class="br0">)</span>
    <span class="br0">{</span>
    <span class="kw3">die</span><span class="br0">(</span><span class="st0">"Admin - Magento<span class="es1">\\</span>CustomControllers<span class="es1">\\</span>Controller<span class="es1">\\</span>Adminhtml<span class="es1">\\</span>Demonstration<span class="es1">\\</span>Sayadmin - execute() method"</span><span class="br0">)</span><span class="sy0">;</span>
    <span class="br0">}</span>
    <span class="br0">}</span>


    Note that by default _isAllowed will return true, and for creating custom permission you must add check under that method. For demonstration we will keep default value so user can access that controller.

    Note: after you add new controller, flush everything from console in Magento root:



    php bin<span class="sy0">/</span>magento setup:upgrade

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •