Controller in Magento 2

Controller in Magento 2 – In this tutorial, we’re creating a magento 2 module, a “Hello World”-style module.

The goal of the module will be simply to write some sentences to the website.

Before we begin:

  • Set developer mode.
    php bin/magento deploy:mode:set deveploper
  • Disable the Cache : you can do this by going to Admin Panel -> System -> Cache Management, Select All -> Actions:Disable and Submit

Let’s do it step by step.

  1. Create all directories and files.
    Magento 2 module structure

Please note :
– No more local, community, or even core code pools. All modules are in /app/code directory.
– registration.php is an important file and it’s required in a magento 2 release version. Its content:

2. Let’s create /app/code/MagentoSharing/Example/etc/module.xml with content:

 

Magento 2 module structure
Magento 2 module structure

Right after creating the file, run the command :
php bin/magento setup:upgrade
Magento can recognize your module now. You can check it by going to Admin Panel -> Stores -> Configuration -> Advanced -> Advanced.

3. Create file /app/code/MagentoSharing/Example/etc/frontend/routes.xml with this content:

You can use your own vendor and module name, of course.

4. We create a file to execute the action of URL : http://localhost/magento2/example/index/index
Create file  with content:

 

Go to http://localhost/magento2/example/, if you got the message that means your action is recognized.

In next tutorial, we’ll set layout for this action. Good luck!

Please leave a comment, thanks.

3 thoughts on “Controller in Magento 2

Leave a Reply

Your email address will not be published.