Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Magento 2 Controller Post throwing 404 error

Tags:

php

magento2

I've hit an issue trying to add a new action to an existing route in Magento 2.

I've created a new module, registered it and created a new action extending the existing add to cart functionality. I had this working on an existing project, but had to port it over to a new installation. Now when I attempt to POST to the path: <magentourl>/checkout/cart/addbulk, it throws a 404. A GET request will hit the execute method and creates the log entry.

The following are the components of the module.

app/code/Test/Checkout/etc/module.xml

<?xml version="1.0"?>
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:Module/etc/module.xsd">
    <module name="Test_Checkout" schema_version="2.0.0" setup_version="2.0.3">
        <sequence>
            <module name="Magento_Checkout"/>
        </sequence>
    </module>
</config>

app/code/Test/Checkout/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="checkout" frontName="checkout">
            <module name="Test_Checkout" />
        </route>
    </router>
</config>

app/code/Test/Checkout/Controller/Cart/Addbulk.php

<?php
namespace Test\Checkout\Controller\Cart;

class Addbulk extends \Magento\Checkout\Controller\Cart\Add
{
    
    public function execute()
    {
        $items = $this->getRequest()->getParam('item', []);
        $this->_objectManager->get('Psr\Log\LoggerInterface')>addDebug('Posting data to addBulk');
        
        ...

        return $this->_goBack();
    }
}

I've enabled the module, run bin/magento setup:upgrade, cleared cache and then attempted to post. When I do a POST, it returns a 404 error, on a GET it creates the log entry as expected.

Does anyone have any idea why this module would not have migrated correctly, or anything else I would need to add to get this working?

like image 593
Piotr Burdzinski Avatar asked Jun 19 '26 15:06

Piotr Burdzinski


1 Answers

Turns out the environment itself was broken. I installed my module on a fresh install of magento 2 successfully.

like image 183
Piotr Burdzinski Avatar answered Jun 22 '26 11:06

Piotr Burdzinski



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!