I have created a basic module that extends the contact form, the issue is that when I try to view the form I get a 404, no errors just 404.
What I want to achieve is a trade account form which looks like a register form but it's really the contact form just with more fields.
I have created my module within the /app/code folder and called it Contact2020/Trade with the following inside:
Block Folder has the file ContactForm.php with the following:
<?php
/**
* Copyright © Magento, Inc. All rights reserved.
* See COPYING.txt for license details.
*/
namespace Contact2020\Trade\Block;
use Magento\Framework\View\Element\Template;
/**
* Main contact form block
*
* @api
* @since 100.0.2
*/
class ContactForm extends Template
{
/**
* @param Template\Context $context
* @param array $data
*/
public function __construct(Template\Context $context, array $data = [])
{
parent::__construct($context, $data);
$this->_isScopePrivate = true;
}
/**
* Returns action url for contact form
*
* @return string
*/
public function getFormAction()
{
return $this->getUrl('trade/index/register', ['_secure' => true]);
}
}
Then I have the Controller/Index folder with Register.php file with the following:
<?php
/**
*
* Copyright © Magento, Inc. All rights reserved.
* See COPYING.txt for license details.
*/
namespace Contact2020\Trade\Controller\Index;
use Magento\Framework\App\Action\HttpGetActionInterface as HttpGetActionInterface;
use Magento\Framework\Controller\ResultFactory;
class Register extends \Magento\Contact\Controller\Index implements HttpGetActionInterface
{
/**
* Show Contact Us page
*
* @return \Magento\Framework\Controller\ResultInterface
*/
public function execute()
{
return $this->resultFactory->create(ResultFactory::TYPE_PAGE);
}
}
I then have etc/frontend with routes.xml with the following:
<!--
/**
* Copyright © Magento, Inc. 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="trade" frontName="trade">
<module name="Contact2020_Trade" />
</route>
</router>
</config>
I have got view/frontend/layout with trade_index_register.xml which has the following:
<?xml version="1.0"?>
<!--
/**
* Copyright © Magento, Inc. All rights reserved.
* See COPYING.txt for license details.
*/
-->
<page xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" layout="1column" xsi:noNamespaceSchemaLocation="urn:magento:framework:View/Layout/etc/page_configuration.xsd">
<head>
<title>Trade Account</title>
</head>
<body>
<referenceContainer name="content">
<block class="Contact2020\Trade\Block\ContactForm" name="contactForm" template="Contact2020_Trade::form.phtml">
<container name="form.additional.info" label="Form Additional Info"/>
</block>
</referenceContainer>
</body>
</page>
And lastly I have /view/frontend/templates with form.phtml which just has my html form:
<form class="form tradeform"
action="<?= $block->escapeUrl($block->getFormAction()) ?>"
id="trade-form"
method="post"
data-hasrequired="<?= $block->escapeHtmlAttr(__('* Required Fields')) ?>"
data-mage-init='{"validation":{}}'>
<fieldset class="fieldset">
<div class="field companyname required">
<label class="label" for="companyname"><span><?= $block->escapeHtml(__('Company Name')) ?></span></label>
<div class="control">
<input name="companyname" id="companyname" title="<?= $block->escapeHtmlAttr(__('Company Name')) ?>" value="<?= $block->escapeHtmlAttr($this->helper(\Magento\Contact\Helper\Data::class)->getPostValue('companyname') ?: $this->helper(\Magento\Contact\Helper\Data::class)->getUserName()) ?>" class="input-text" type="text" data-validate="{required:true}"/>
</div>
</div>
<div class="field companynumber required">
<label class="label" for="companynumber"><span><?= $block->escapeHtml(__('Company Ltd Number')) ?></span></label>
<div class="control">
<input name="companynumber" id="companynumber" title="<?= $block->escapeHtmlAttr(__('Company Ltd Number')) ?>" value="<?= $block->escapeHtmlAttr($this->helper(\Magento\Contact\Helper\Data::class)->getPostValue('companynumber') ?: $this->helper(\Magento\Contact\Helper\Data::class)->getUserName()) ?>" class="input-text" type="text" data-validate="{required:true}"/>
</div>
</div>
<div class="field tradingaddress required">
<label class="label" for="tradingaddress"><span><?= $block->escapeHtml(__('Company Trading Address')) ?></span></label>
<div class="control">
<input name="tradingaddress" id="tradingaddress" title="<?= $block->escapeHtmlAttr(__('Company Trading Address')) ?>" value="<?= $block->escapeHtmlAttr($this->helper(\Magento\Contact\Helper\Data::class)->getPostValue('tradingaddress') ?: $this->helper(\Magento\Contact\Helper\Data::class)->getUserName()) ?>" class="input-text" type="text" data-validate="{required:true}"/>
</div>
</div>
<div class="field companycontactnumber required">
<label class="label" for="companycontactnumber"><span><?= $block->escapeHtml(__('Company Contact Number')) ?></span></label>
<div class="control">
<input name="companycontactnumber" id="companycontactnumber" title="<?= $block->escapeHtmlAttr(__('Company Contact Number')) ?>" value="<?= $block->escapeHtmlAttr($this->helper(\Magento\Contact\Helper\Data::class)->getPostValue('companycontactnumber') ?: $this->helper(\Magento\Contact\Helper\Data::class)->getUserName()) ?>" class="input-text" type="text" data-validate="{required:true}"/>
</div>
</div>
<div class="field companyemail required">
<label class="label" for="companyemail"><span><?= $block->escapeHtml(__('Company Contact Email')) ?></span></label>
<div class="control">
<input name="email" id="companyemail" title="<?= $block->escapeHtmlAttr(__('Company Contact Email')) ?>" value="<?= $block->escapeHtmlAttr($this->helper(\Magento\Contact\Helper\Data::class)->getPostValue('companyemail') ?: $this->helper(\Magento\Contact\Helper\Data::class)->getUserEmail()) ?>" class="input-text" type="email" data-validate="{required:true, 'validate-email':true}"/>
</div>
</div>
<div class="field fullname required">
<label class="label" for="fullname"><span><?= $block->escapeHtml(__('Full Name')) ?></span></label>
<div class="control">
<input name="fullname" id="fullname" title="<?= $block->escapeHtmlAttr(__('Full Name')) ?>" value="<?= $block->escapeHtmlAttr($this->helper(\Magento\Contact\Helper\Data::class)->getPostValue('fullname') ?: $this->helper(\Magento\Contact\Helper\Data::class)->getUserName()) ?>" class="input-text" type="text" data-validate="{required:true}"/>
</div>
</div>
<div class="field birthday required">
<label class="label" for="birthday"><span><?= $block->escapeHtml(__('DD')) ?></span></label>
<div class="control">
<input name="birthday" id="birthday" title="<?= $block->escapeHtmlAttr(__('DD')) ?>" value="<?= $block->escapeHtmlAttr($this->helper(\Magento\Contact\Helper\Data::class)->getPostValue('birthday') ?: $this->helper(\Magento\Contact\Helper\Data::class)->getUserName()) ?>" class="input-text" type="text" data-validate="{required:true}"/>
</div>
</div>
<div class="field birthmonth required">
<label class="label" for="birthmonth"><span><?= $block->escapeHtml(__('MM')) ?></span></label>
<div class="control">
<input name="birthmonth" id="birthmonth" title="<?= $block->escapeHtmlAttr(__('MM')) ?>" value="<?= $block->escapeHtmlAttr($this->helper(\Magento\Contact\Helper\Data::class)->getPostValue('birthmonth') ?: $this->helper(\Magento\Contact\Helper\Data::class)->getUserName()) ?>" class="input-text" type="text" data-validate="{required:true}"/>
</div>
</div>
<div class="field birthyear required">
<label class="label" for="birthyear"><span><?= $block->escapeHtml(__('YYYY')) ?></span></label>
<div class="control">
<input name="birthyear" id="birthyear" title="<?= $block->escapeHtmlAttr(__('YYYY')) ?>" value="<?= $block->escapeHtmlAttr($this->helper(\Magento\Contact\Helper\Data::class)->getPostValue('birthyear') ?: $this->helper(\Magento\Contact\Helper\Data::class)->getUserName()) ?>" class="input-text" type="text" data-validate="{required:true}"/>
</div>
</div>
<div class="field address required">
<label class="label" for="address"><span><?= $block->escapeHtml(__('Address')) ?></span></label>
<div class="control">
<textarea name="address" id="address" title="<?= $block->escapeHtmlAttr(__('Address')) ?>" class="input-text" cols="5" rows="5" data-validate="{required:true}"><?= $block->escapeHtml($this->helper(\Magento\Contact\Helper\Data::class)->getPostValue('address')) ?></textarea>
</div>
</div>
<div class="field address2 required">
<label class="label" for="address2"><span><?= $block->escapeHtml(__('Address (If not lived at current address for 3 years or more)')) ?></span></label>
<div class="control">
<textarea name="address2" id="address2" title="<?= $block->escapeHtmlAttr(__('Address (If not lived at current address for 3 years or more)')) ?>" class="input-text" cols="5" rows="5" data-validate="{required:true}"><?= $block->escapeHtml($this->helper(\Magento\Contact\Helper\Data::class)->getPostValue('address2')) ?></textarea>
</div>
</div>
<div class="field contacttelephone">
<label class="label" for="contacttelephone"><span><?= $block->escapeHtml(__('Contact Number')) ?></span></label>
<div class="control">
<input name="contacttelephone" id="contacttelephone" title="<?= $block->escapeHtmlAttr(__('Contact Number')) ?>" value="<?= $block->escapeHtmlAttr($this->helper(\Magento\Contact\Helper\Data::class)->getPostValue('contacttelephone')) ?>" class="input-text" type="text" />
</div>
</div>
<div class="field contactemail required">
<label class="label" for="contactemail"><span><?= $block->escapeHtml(__('Contact Email')) ?></span></label>
<div class="control">
<input name="email" id="contactemail" title="<?= $block->escapeHtmlAttr(__('Contact Email')) ?>" value="<?= $block->escapeHtmlAttr($this->helper(\Magento\Contact\Helper\Data::class)->getPostValue('contactemail') ?: $this->helper(\Magento\Contact\Helper\Data::class)->getUserEmail()) ?>" class="input-text" type="email" data-validate="{required:true, 'validate-email':true}"/>
</div>
</div>
<div class="field tradingname required">
<label class="label" for="tradingname"><span><?= $block->escapeHtml(__('Trading Name')) ?></span></label>
<div class="control">
<input name="tradingname" id="tradingname" title="<?= $block->escapeHtmlAttr(__('Trading Name')) ?>" value="<?= $block->escapeHtmlAttr($this->helper(\Magento\Contact\Helper\Data::class)->getPostValue('tradingname') ?: $this->helper(\Magento\Contact\Helper\Data::class)->getUserName()) ?>" class="input-text" type="text" data-validate="{required:true}"/>
</div>
</div>div>
</div>
<?= $block->getChildHtml('form.additional.info') ?>
</fieldset>
<div class="actions-toolbar">
<div class="primary">
<input type="hidden" name="hideit" id="hideit" value="" />
<button type="submit" title="<?= $block->escapeHtmlAttr(__('Submit')) ?>" class="action submit primary">
<span><?= $block->escapeHtml(__('Submit')) ?></span>
</button>
</div>
</div>
</form>
If I navigate to http://localhost:8888/store/trade/index/register I get a 404, but If I navigate to http://localhost:8888/store/contact/index/index I can see the default contact form. I have run these commands in my console:
php -d memory_limit=2048M bin/magento setup:upgrade php -d
memory_limit=2048M bin/magento setup:di:compile php -d
memory_limit=2048M bin/magento cache:flush
Is there something I'm missing?