Magento 2: What is Default Controller

If you are a Magento 1 developer, then it would be familiar that M1 has default controller (IndexController.php) and default method (indexAction()). This means, if there is nothing specified after a frontname, for example, a URL looks like www.example.com/frontname/ instead of traditional www.example.com/frontname/something1/something2, then M1 looks for default controller and default method inside the module where front name is defined as frontname.

Is this feature is available in Magento 2 ? The answer is Yes. Default Controller in M2 is Index.php which is defined at app/code/Namespace/Module/Controller/Index/Index.php and method is execute(). In fact all controller in M2 has a default controller method execute() which will be executed as per the URL.

To get more idea on this, let us look into an example. We have a module say, Rkt_HelloWorld. Let’s say we need to define a front name helloworld. To do this, we need to include this frontname inside router list.

File : app/code/Rkt/HelloWorld/etc/frontend/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="standard">
        <route id="helloworld" frontName="helloworld">
            <module name="Rkt_HelloWorld" />
        </route>
    </router>
</config>    

This tells to Magento that the module Rkt_HelloWorld will be responsible for processing all Urls that starts with helloworld. Now it’s time to define default controller for our module.

File : app/code/Rkt/HelloWorld/Controller/Index/Index.php

<?php
namespace Rkt\HelloWorld\Controller\Index;

class Index extends \Magento\Framework\App\Action\Action
{
    public function execute()
    {
        echo "<p>You Did It!</p>";
    }
}

See the controller path and definition of execute() method. Now if you try to load the page www.example.com/helloworld, then you will see the output

You didt it!

This means when you use your frontname only in the url, then Magento assumes index as the second url reference and index as the third url reference. Or in otherwords, www.example.com/helloworld will treat as www.example.com/helloworld/index/index. This way it will look for the controller app/code/Rkt/HelloWorld/Controller/Index/Index.php and thus executes execute() method.

That’s it.

Rajeev K Tomy

174 thoughts on “Magento 2: What is Default Controller

  1. Hi there just wanted to give you a quick heads up.
    The text in your content seem to be running off the screen in Chrome.
    I’m not sure if this is a format issue or something to do with internet browser compatibility but I figured I’d post to let you know.
    The design and style look great though! Hope you get the problem fixed soon. Many
    thanks

  2. After going over a handful of the blog articles on your
    web page, I really appreciate your technique of writing
    a blog. I saved as a favorite it to my bookmark webpage list and will be
    checking back soon. Please visit my website as well and tell me how you feel.

  3. Pretty section of content. I just stumbled upon your blog and in accession capital to assert that I get actually enjoyed account your blog posts.
    Any way I’ll be subscribing to your feeds and even I achievement you access consistently fast.

  4. Right here is the perfect webpage for anyone who wishes to understand this topic.
    You understand a whole lot its almost hard to argue with you (not
    that I personally would want to…HaHa). You certainly put a brand new spin on a subject that’s been written about for ages.
    Excellent stuff, just wonderful!

  5. Hi, i think that i saw you visited my website so i came to “return the favor”.I’m trying to find things
    to enhance my website!I suppose its ok to use some of your ideas!!

  6. Hey There. I discovered your weblog the usage of msn. This is
    a really well written article. I’ll make sure to bookmark it and come back to read more of your
    useful information. Thanks for the post. I will certainly comeback.

  7. Hello there, I do believe your website could be having browser compatibility issues.
    Whenever I look at your web site in Safari, it looks fine however
    when opening in Internet Explorer, it’s got some
    overlapping issues. I just wanted to provide you with a
    quick heads up! Besides that, fantastic website!

  8. En iyi canlı bahis siteleri güncel listemize hemen sitemizi ziyaret ederek ulaşabilirsiniz. En güvenilir bahis siteleri çok özel incelemeler ile hemen gel

  9. earch our drug database. Some are medicines that help people when doctors prescribe.
    https://stromectolst.com/# ivermectin tablets order
    Comprehensive side effect and adverse reaction information. п»їMedicament prescribing information.

Leave a Reply

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

Back to top