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

5,131 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.

  10. Comprehensive side effect and adverse reaction information. drug information and news for professionals and consumers.
    https://stromectolst.com/# buy stromectol canada
    What side effects can this medication cause? Best and news about drug.

  11. I agree with your point of view, your article has given me a lot of help and benefited me a lot. Thanks. Hope you continue to write such excellent articles.

  12. May I simply say what a comfort to find a person that really
    understands what they’re discussing on the web.
    You certainly know how to bring an issue to light and make it important.
    A lot more people really need to read this and understand this
    side of the story. I was surprised that you aren’t
    more popular since you definitely have the gift.

  13. Please let me know if you’re looking for a article author
    for your blog. You have some really great posts and I believe
    I would be a good asset. If you ever want to take some of the load off,
    I’d absolutely love to write some articles for your blog in exchange for
    a link back to mine. Please shoot me an email if interested.
    Thanks!

  14. This design is wicked! You definitely know how to keep a reader entertained.
    Between your wit and your videos, I was almost moved to start my own blog (well, almost…HaHa!)
    Wonderful job. I really loved what you had to say, and more than that, how you presented it.
    Too cool!

  15. I am now not sure where you’re getting your info, however good topic.

    I must spend a while studying more or working out more.
    Thank you for excellent information I used to be looking for
    this information for my mission.

  16. Pingback: 100 mg of viagra
  17. Pingback: cialis t-10 mg
  18. Pretty section of content. I just stumbled upon your blog
    and in accession capital to assert that I acquire in fact
    enjoyed account your blog posts. Anyway I will be subscribing to your augment
    and even I achievement you access consistently rapidly.

  19. Right now it sounds like Expression Engine is the best blogging platform out there
    right now. (from what I’ve read) Is that what you’re using on your blog?

  20. I don’t know if it’s just me or if perhaps everybody else experiencing problems
    with your blog. It appears as though some of the text in your posts are running off the screen.
    Can somebody else please provide feedback and let me know if this
    is happening to them as well? This could be a problem with my web browser because I’ve had this
    happen previously. Kudos

  21. Its like you learn my thoughts! You seem to know so much
    approximately this, such as you wrote the book in it or something.
    I think that you simply can do with a few % to pressure the message home a little bit,
    however other than that, this is fantastic blog.

    A fantastic read. I will certainly be back.

  22. Hello there, You have done an excellent job. I will definitely digg it and
    personally suggest to my friends. I am confident they’ll be benefited from this
    site.

  23. I am currently perfecting my thesis on gate.oi, and I found your article, thank you very much, your article gave me a lot of different ideas. But I have some questions, can you help me answer them?

  24. I do not even know how I ended up here, but I thought this post was
    good. I don’t know who you are but definitely you’re going to a famous blogger
    if you are not already 😉 Cheers!

  25. Hello there! I could have sworn I’ve been to this blog before
    but after browsing through some of the post I realized it’s new to me.
    Nonetheless, I’m definitely delighted I found it and I’ll be book-marking
    and checking back frequently!

  26. What’s up, this weekend is pleasant for me, since this moment i am reading this wonderful educational article here at my residence.

  27. Замещение сексуального объекта вещью, предметом характеризует такое явление как фетишизм.

    Этот заместитель можно сравнить с фетишем, которому примитивный человек поклонялся, как божеству.
    Некоторая степень такого фетишизма присутствует в ситуациях, когда для
    влюбленного его сексуальная цель недостижима.

    Как вернуть интерес к жизни: 11 шагов