Piotr Burdzinski
Piotr Burdzinski

Reputation: 41

Magento 2 Controller Post throwing 404 error

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?

Upvotes: 3

Views: 7777

Answers (3)

marmureanuweb
marmureanuweb

Reputation: 36

bin/magento cache:disable

Clear the contents of folders

generated/code and generated/metadata.

Check PHP-FPM or OPCACHE cache also.

Upvotes: 0

Piotr Burdzinski
Piotr Burdzinski

Reputation: 41

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

Upvotes: 1

Paul T. Rawkeen
Paul T. Rawkeen

Reputation: 4114

Did you try this after creating your controller?

bin/magento setup:di:compile

Or enabling Developer mode?

Upvotes: 0

Related Questions