0

I have created a custom controller to show a custom form but it goes to 404 page in Magento 2 ?

Controller file path : ( app/code/Company/Module/Controller/Index/Booking.php )

<?php

namespace Company\Module\Controller\Index;

use Magento\Framework\Controller\ResultFactory;
use Magento\Framework\View\Result\PageFactory;
use Magento\Framework\App\Action\Context;

class Booking extends \Magento\Framework\App\Action\Action
{
protected $customerSession;
protected $urlInterface;

public function __construct(
    \Magento\Customer\Model\Session $customerSession,
    \Magento\Framework\UrlInterface $urlInterface,
    \Magento\Framework\App\Action\Context $context
) {
    $this->urlInterface = $urlInterface;
    $this->customerSession = $customerSession; 
    parent::__construct($context);
}
/**
 * Booking action
 *
 * @return void
 */
public function execute()
{
    if(!$this->customerSession->isLoggedIn()) {
        $this->messageManager->addErrorMessage("You must be logged in to view product");
        $this->customerSession->setAfterAuthUrl($this->urlInterface->getCurrentUrl());
        $this->customerSession->authenticate();
        return;
    }
    // 1. POST request : Get booking data
    $post = (array) $this->getRequest()->getPost();

    if (!empty($post)) {
        // Retrieve your form data
        $firstname   = $post['firstname'];
        $lastname    = $post['lastname'];
        $phone       = $post['phone'];
        $bookingTime = $post['bookingTime'];

        // Doing-something with...

        // Display the succes form validation message
        $this->messageManager->addSuccessMessage('Booking done !');

        // Redirect to your form page (or anywhere you want...)
        $resultRedirect = $this->resultFactory->create(ResultFactory::TYPE_REDIRECT);
        $resultRedirect->setUrl('/companymodule/index/booking');

        return $resultRedirect;
    }
    // 2. GET request : Render the booking page 
    $this->_view->loadLayout();
    $this->_view->renderLayout();
}
}

My routes file path ( app/code/Company/Module/etc/frontend/routes.xml )

<?xml version="1.0" encoding="UTF-8"?>
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
xsi:noNamespaceSchemaLocation="urn:magento:framework:App/etc/routes.xsd">
<router id="standard">
    <route id="companymodule" frontName="companymodule">
        <module name="Company_Module"/>
    </route>
</router>
</config>

My companymodule_index_booking.xml file path ( app/code/Company/Module/view/frontend/layout/companymodule_index_booking.xml )

<?xml version="1.0"?>
<page layout="2columns-left" 
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"xsi:noNamespaceSchemaLocation="urn:magento:framework:View/Layout/etc/page_configuration.xsd">
<head>
    <title>HTML title - The booking form page</title>
</head>
<body>
    <referenceBlock name="navigation.sections" remove="true" />
    <referenceContainer name="content">
        <block class="Company\Module\Block\Booking" 
name="companymodule.booking" template="Company_Module::booking.phtml"/>
    </referenceContainer>
</body>
</page>

My Block code path ( app/code/Company/Module/Block/Booking.php )

<?php

namespace Company\Module\Block;

class Booking extends \Magento\Framework\View\Element\Template
{
/**
 * Construct
 *
 * @param \Magento\Framework\View\Element\Template\Context $context
 * @param array $data
 */
public function __construct(
    \Magento\Framework\View\Element\Template $context,
    array $data = []
)
{
    parent::__construct($context, $data);
   }

/**
 * Get form action URL for POST booking request
 *
 * @return string
 */
public function getFormAction()
{
        // companymodule is given in routes.xml
        // controller_name is folder name inside controller folder
        // action is php file name inside above controller_name folder

    return '/companymodule/controller_name/action';
    // here controller_name is index, action is booking
}
}

I have followed this How to Create Custom Form in Magento2 Frontend? for that but 404 comes how to solve that error. Any help is appriciated.

2
  • Please share your block code Aug 26, 2020 at 9:36
  • Updated please check @HarshJayswal
    – Prits
    Aug 26, 2020 at 9:40

2 Answers 2

0

Change your controller like this :

<?php
namespace Company\Module\Controller\Index;

use Magento\Framework\Controller\ResultFactory;

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

And then check does your URL is access or not.

5
  • It gives me an error on hitting url " Invalid block type: Company\Module\Block\Booking "
    – Prits
    Aug 26, 2020 at 10:39
  • Confirm you are creating this block class : Company\Module\Block\Booking Aug 26, 2020 at 10:44
  • Yes basically i want a custom form on froentend @Dhiren
    – Prits
    Aug 26, 2020 at 10:47
  • I telling that confirm you are passing proper class in your xml and that block file is exist on the same location, because error is that that class not found. Aug 26, 2020 at 12:07
  • Thanks for your suggestion there is an mismatched class name in my Block file. On submitting the form it goes to 127.0.0.1/companymodule/index/booking and not found and on applying die on it it doesn't show there.will you please help me @Dhiren
    – Prits
    Aug 26, 2020 at 12:22
0

Please update the below files

Block

<?php 
namespace namespace Company\Module\Block;


class Booking extends \Magento\Framework\View\Element\Template
{
public function __construct(
    \Magento\Framework\View\Element\Template\Context $context
) {
    parent::__construct($context);
}

public function _prepareLayout()
{   
    return parent::_prepareLayout();
}
}

Controller

<?php
namespace Company\Module\Controller\Index;

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

Layout

<?xml version="1.0"?>
<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="Company\Module\Block\Booking" name="booking" template="booking.phtml">
        </block>
    </referenceContainer>
</body>

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.