Creating a web route and controller to display data

In order to display data from your extension on the frontend (the public part of the website), the following is necessary:

  • A configured route
  • A controller handling the request
  • A layout file to specify what to show
  • The block class as specified in the layout file
  • A template file (optional)

How to do it…

Follow these steps to extend your module with a frontend web route and output data from a template file:

  1. Create a route in the frontend area:

    etc/frontend/routes.xml

    <?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="sample" frontName="sample"> <module name="Genmato_Sample" ...

Get Magento 2 - Build World-Class online stores now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.