Magento 2 – Custom Controller Not Executing

controllersmagento2module

Controller is calling, but .phtml file is not calling. Even though I have debugging both controller and .phtml files controller is accessing through debugging but .phtml file is not accessing.

Helloworld/Trackorder/Block/Track.php

<?php

namespace Helloworkd\Trackorder\Block;

use Magento\Framework\View\Element\Template;

class Track extends Template{

    /**
     * @var \Magento\Framework\Registry $registry
     */
    protected $_registry;

    public function __construct
    (
        Template\Context $context,
        \Magento\Framework\Registry $registry,
        array $data = []
    )
    {
        $this->_registry = $registry;
        parent::__construct($context, $data);
    }


}

Helloworld/Trackorder/Controller/Index/Index.php

<?php
namespace Helloworkd\Trackorder\Controller\Index;

class Index extends \Magento\Framework\App\Action\Action
{

    /** @var  \Magento\Framework\View\Result\Page */
    protected $resultPageFactory;


    /**
     * @param \Magento\Framework\App\Action\Context $context
     * @param \Magento\Framework\View\Result\PageFactory $resultPageFactory
     */
    public function __construct(
        \Magento\Framework\App\Action\Context $context,
        \Magento\Framework\View\Result\PageFactory $resultPageFactory)
    {
        $this->resultPageFactory = $resultPageFactory;
        parent::__construct($context);
    }

    /**
     * Track Order.
     *
     * @return \Magento\Framework\View\Result\PageFactory
     */
    public function execute()
    {
        return $this->resultPageFactory->create();
    }

}

Helloworld/Trackorder/etc/frontend/routes.xml

<?xml version="1.0"?>
<!--
/**
 * Copyright © 2015 Magento. All rights reserved.
 * See COPYING.txt for license details.
 */
-->
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:App/etc/routes.xsd">
    <router id="standard">
        <route id="trackorder" frontName="trackorder">
            <module name="Helloworld_Trackorder" />
        </route>
    </router>
</config>

Helloworld/Trackorder/view/frontend/layout/trackorder_index_index.xml

<?xml version="1.0"?>
<page layout="1column" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:View/Layout/etc/page_configuration.xsd">
    <head>
        <title>Track Order</title>
    </head>
    <body>
        <referenceBlock name="track_order">
            <block class="Helloworkd\Trackorder\Block\Track" name="custom_track_order" template="Helloworld_Trackorder::trackorder.phtml"></block>
        </referenceBlock>
    </body>
</page>

Helloworld/Trackorder/view/frontend/templates/trackorder.phtml

<?php

if($this->helper('Helloworkd\Trackorder\Helper\Data')->getModule()){?>

    <div class="page-title"><h1><?php echo __('Track Your Order ') ?></h1></div>
    <div class="form-list">
        <form name="track_order" id="track_order" action="" method="post" >
            <ul class="form-list">
                <li>
                    <label for="order_id" class="required"><em>*</em><?php echo __('Order Id') ?></label>
                    <div class="input-box">
                        <input type="text" name="order_id" id="order_id" value="" title="" class="input-text required-entry" />
                    </div>
                </li>
                <li>
                    <label for="email_address" class="required"><em>*</em><?php echo __('Email Address') ?></label>
                    <div class="input-box" >
                        <input type="text" name="email" id="email_address" value="" title="<?php echo __('Email Address') ?>" class="input-text validate-email required-entry" />
                    </div>
                </li>
            </ul>
            <div class="buttons-set">
                <button type="submit" class="button button-left" title="<?php echo __('Track Order') ?>" name="track" id="track"><span><span><?php echo __('Track Order') ?></span></span></button>
            </div>

        </form>
        <div id="loading-details" class="loading-details" style="display:none">
            <div id="loading-mask" >
                <p class="loader" id="loadmask"><img src="<?php echo $this->getViewFileUrl('images/ajax-loader-tr.gif') ?>" alt="<?php __('Loading...') ?>"/><br/><?php echo __('Please wait...') ?></p>
            </div>
        </div>
    </div>

    <div id="oderinfo" class="order-info-message"></div>

<?php } ?>

enter image description here

Could you please suggest me where I went wrong?

Best Answer

change your code from trackorder_index_index.xml

<?xml version="1.0"?>
<page layout="1column" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:View/Layout/etc/page_configuration.xsd">
    <head>
        <title>Track Order</title>
    </head>
    <body>
        <referenceBlock name="track_order">
            <block class="Helloworkd\Trackorder\Block\Track" name="custom_track_order" template="Helloworld_Trackorder::trackorder.phtml"></block>
        </referenceBlock>
    </body>
</page>

to

<?xml version="1.0"?>
<page layout="1column" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:View/Layout/etc/page_configuration.xsd">
    <head>
        <title>Track Order</title>
    </head>
    <body>
        <referenceBlock name="content">
            <block class="Helloworkd\Trackorder\Block\Track" name="custom_track_order" template="Helloworld_Trackorder::trackorder.phtml"></block>
        </referenceBlock>
    </body>
</page>

<referenceBlock name="track_order"> should be <referenceBlock name="content">

Related Topic