Magento 2 Training : Unit 2 – Lesson B

We have declared and created our new custom module. We will learn how to create a frontend controller. The controller can call our module with an URL like :
http://magento2.lan/helloworld
http://magento2.lan/helloworld/say
http://magento2.lan/helloworld/say/hello

Router declaration

In order to tell Magento the routes you want to create, we will create a XML file :
app/code/Maxime/Helloworld/etc/frontend/routes.xml

Put the following content :

<?xml version="1.0"?>
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:App/etc/routes.xsd">
	<router id="standard">
		<route id="helloworld" frontName="helloworld">
			<module name="Maxime_Helloworld" before="Magento_Core" after="Magento_Sales" />
		</route>
	</router>
</config>

As you can see, we precise a XSD file which define the XML structure.

After, we have the sequence router id="standard", which says it’s a frontend controller, contrary to the admin controller for the Magento backoffice.

The composition of the node route is :
– id : unique identifier for your router
– frontName : the string which be used on the front URL

Then, there is the module node with these attributes:
– name : module name to call
– before : your module have to be called before the module defined inside
– after : you module have to be called after the module defined inside

“before” et “after” fields are NOT required

Controller creation

We have explained to Magento the road to take, so let’s create it now. We will create an URL like :
http://magento2.lan/helloworld

Create required folders to create the following file :
app/code/Maxime/Helloworld/Controller/Index/Index.php

Put this content inside :

<?php
namespace Maxime\Helloworld\Controller\Index;
class Index extends \Magento\Framework\App\Action\Action
{
    public function execute()
    {
       echo 'Execute Action Index_Index OK';
       die();
    }
}

The namespace is the path of your controller inside the app/code folder.
The class name is the name of your PHP file.

Now, type this URL on your browser :
http://magento2.lan/helloworld

You’ll have a blank page with the message : Execute Action Index_Index OK

If you have an error like :
Please upgrade your database: Run "bin/magento setup:upgrade" from the Magento root directory.
Go on your Magento root folder and execute this command :

./bin/magento setup:upgrade

Customize URL

Our previous URL is very simple :
http://magento2.lan/helloworld
But Magento 2 convert it to :
http://magento2.lan/helloworld/index/index

The first “index” is the folder “Index” inside “Controller”
The second “index” is the file “Index.php” on the “Index” folder.

You can guess how to create the following pathes :
http://magento2.lan/helloworld/say
http://magento2.lan/helloworld/say/hello

You can search from yourself in a first time, and get the solution below :

For URL http://magento2.lan/helloworld/say

– Create a folder named “Say” and a “Index” PHP file inside :
app/code/Maxime/Helloworld/Controller/Say/Index.php

We will put this code :

<?php
namespace Maxime\Helloworld\Controller\Say;
class Index extends \Magento\Framework\App\Action\Action
{
    public function execute()
    {
       echo 'Execute Action Say_Index OK';
       die();
    }
}

For URL http://magento2.lan/helloworld/say/hello

– Create a folder named “Say” and a “Hello” PHP file inside
app/code/Maxime/Helloworld/Controller/Say/Hello.php

We will put this code :

<?php
namespace Maxime\Helloworld\Controller\Say;
class Hello extends \Magento\Framework\App\Action\Action
{
    public function execute()
    {
       echo 'Execute Action Say_Hello OK';
       die();
    }
}

This is not very beautiful, but in the next lesson, we will learn how to create a block with Magento 2.

Continue training
Return to previous lesson
Create Magento 2 frontend controller
Tagged on:     

6 thoughts on “Create Magento 2 frontend controller

  • 02/17/2016 at 12:25
    Permalink

    After command “bin/magento setup:upgrade” blank pages on site (admin, front)

    Reply
  • 03/20/2016 at 11:15
    Permalink

    I have this error http://joxi.ru/823pK6NI65P6XA, with Controller http://joxi.ru/zANVjJKFlKelLA. But i fixed id by this code: http://joxi.ru/82Q5N0Rs1REjj2, and this steps:
    1. sudo rm -Rf var/generation/*
    2.sudo rm -Rf var/di
    3. sudo rm -Rf var/cache/*
    4.sudo rm -Rf var/page_cache/*
    5.php ./bin/magento setup:di:compile
    6.chmod -R 777 *
    Is it correct fix?
    P.S. Dear Max, I have a big problems with permissions:
    I set chown and chmod on the project dir, put my user to the www-data group,
    http://joxi.ru/Grqg9ZQuN63QD2, but every time when I use setup:upgrade or setup:di:compile,
    I see this: http://joxi.ru/brR6pJ3SQ3KYDr , and I can fix it, only by another chmod -R 777 *. What I`m doing wrong?
    http://joxi.ru/nAy8MkaIX37jP2

    Reply
    • 05/19/2016 at 11:41
      Permalink

      I got the same issue like you, bro!

      Reply
    • 05/23/2016 at 15:26
      Permalink

      That’s really strange, I tested the module after de 2.0.6 update and everything is alright with the module code.
      I saw you change the URL of the module, maybe you forget to change some occurences inside the code.

      Reply

Leave a Reply to minimax Cancel reply

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

We use cookies to ensure that we give you the best experience on our website.
Ok