Skip to content

Submitting a form on a CMS page that is loading a template file is returning a 404 error on the post request

I think I am running into a routing issue where my post request is not reaching my controller.

Test/Example/view/frontend/templates/swatches.phtml

<form action="<? echo $block->getUrl('example/index/index'); ?>" id="pgswatchForm" method="post">

Test/Example/Controller/Index/Index.php

<?php
namespace TestExampleControllerIndex;

use MagentoFrameworkAppActionAction;
use MagentoFrameworkAppActionContext;
use MagentoFrameworkViewResultPageFactory;
use MagentoFrameworkMailTemplateTransportBuilder;
use MagentoFrameworkAppActionHttpPostActionInterface;
class Index extends Action implements HttpPostActionInterface
{
    protected $resultPageFactory;
    protected $transportBuilder;

    public function __construct(
        Context $context,
        PageFactory $resultPageFactory,
        TransportBuilder $transportBuilder,
    ) {
        parent::__construct($context);
        $this->resultPageFactory = $resultPageFactory;
        $this->transportBuilder = $transportBuilder;
    }

    public function execute()
    {
        $post = $this->getRequest()->getPostValue();
        
        if ($post) {
           echo 'test';
        }
        die(print_r('we made it to the controller'));
    }

Test/Example/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="example" frontName="example">
            <module name="Test_Example" />
        </route>
    </router>
</config>

Test/Example/Block/Swatches.php

<?php
namespace TestExampleBlock;

use MagentoFrameworkViewElementTemplate;

class Swatches extends Template
{
    public function _prepareLayout()
    {
        return parent::_prepareLayout();
    }
}

And in my CMS page, all I have loaded is

{{block class="WarmothPickguardSwatchesBlockSwatches" template="Warmoth_PickguardSwatches::swatches.phtml"}}

When I submit the form, I get taken to a 404 not found page. The network tool in the console specifies that the POST action on https://site.com/example/index/index was not found.