Magento 2 | Create frontend route

Here is complete example of how the fronted route work it is combination of controller,layout,block and template.

Route

app/code/QaisarSatti/HelloWorld/etc/frontend/routes.xml

<?xml version="1.0"?>
<!--
/**
 * Simple Hello World Module
 *
 * @category QaisarSatti
 * @package QaisarSatti_HelloWorld
 * @author Muhammad Qaisar Satti
 * @Email [email protected]
 *
 */-->   
<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="QaisarSatti_HelloWorld" />
    </route>
  </router>
</config>

Controller

app/code/QaisarSatti/HelloWorld/Controller/Index/Index.php

<?php
/**
 * Simple Hello World Module
 *
 * @category QaisarSatti
 * @package QaisarSatti_HelloWorld
 * @author Muhammad Qaisar Satti
 * @Email [email protected]
 *
 */

namespace QaisarSatti\HelloWorld\Controller\Index;

class Index extends \Magento\Framework\App\Action\Action
{
    public function execute()
    {
        $this->_view->loadLayout();
        $this->_view->renderLayout();
    }
}

Layout File

app/code/QaisarSatti/HelloWorld/view/frontend/layout/helloworld_index_index.xml

<?xml version="1.0"?>
<!--
/**
 * Simple Hello World Module
 *
 * @category QaisarSatti
 * @package QaisarSatti_HelloWorld
 * @author Muhammad Qaisar Satti
 * @Email [email protected]
 *
 */-->
<page 
      xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
      layout="1column" 
      xsi:noNamespaceSchemaLocation="urn:magento:framework:View/Layout/etc/page_configuration.xsd">
  <body>
    <referenceContainer name="content">
      <block 
            class="QaisarSatti\HelloWorld\Block\HelloWorld" 
            name="HelloWorld" 
            template="QaisarSatti_HelloWorld::HelloWorld.phtml">
        .
      </block>
    </referenceContainer>
  </body>
</page>

Template File

app/code/QaisarSatti/HelloWorld/view/frontend/templates/HelloWorld.phtml

<?php
/**
 * Catalog Product Rewrite Helper
 *
 * @category QaisarSatti
 * @package QaisarSatti_HelloWorld
 * @author Muhammad Qaisar Satti
 * @Email [email protected]
 *
 */
echo 'Hello World';

Custom Block

app/code/QaisarSatti/HelloWorld/Block/HelloWorld.php

<?php
/**
 * Simple Hello World Module
 *
 * @category QaisarSatti
 * @package QaisarSatti_HelloWorld
 * @author Muhammad Qaisar Satti
 * @Email [email protected]
 *
 */

namespace QaisarSatti\HelloWorld\Block;

class HelloWorld extends \Magento\Framework\View\Element\Template
{
    public function _prepareLayout()
    {
        parent::_prepareLayout();
        $this->pageConfig->getTitle()->set(__('First Hello World Module'));
        return $this;
    }
}

Reference


create Index.php controller in on below path :

/app/code/UO/NewsletterUV/Controller/Index/

<?php
namespace UO\NewsletterUV\Controller\Index;

use Magento\Framework\App\Action\Action;

class Index extends Action
{
    public function execute()
    {
        echo "Controller call successfully";
    }
}

Try this:

Create PHP file at app/code/UO/NewsletterUV/Controller/Index/ with Index.php.

Code should be like this in that file.

namespace UO\NewsletterUV\Controller\Index;

use Magento\Framework\App\Action\Action;

/**
 * Class Index
 * @package UO\NewsletterUV\Controller\Index\Index
 */
class Index extends Action
{


    /**
     * Function execute
     * @return \Magento\Framework\View\Result\Page
     */
    public function execute()
    {  
        echo "Rout Called";

    }
}