0

I've created and enabled a module at app/code/Post/Test:

app/code/Post/Test/registration.php:

<?php

\Magento\Framework\Component\ComponentRegistrar::register(
    \Magento\Framework\Component\ComponentRegistrar::MODULE,
    'Post_Test',
    __DIR__
);

app/code/Post/Test/etc/module.xml:

<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:Module/etc/module.xsd">
    <module name="Post_Test" setup_version="1.0.0" />
</config>

app/code/Post/Test/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="post_test" frontName="posttest">
            <module name="Post_Test" />
        </route>
    </router>
</config>

app/code/Post/Test/Controller/Test:

<?php

namespace Post\Test\Controller;

use Magento\Framework\Controller\Result\Raw;
use Magento\Framework\App\Action\HttpPostActionInterface;

class Test implements HttpPostActionInterface
{
    private $raw;

    public function __construct(Raw $raw)
    {
        $this->raw = $raw;
    }

    public function execute()
    {
        return $this->raw->setContents('foo bar baz');
    }
}

Based on this, I think that my controller should be accessible via a POST request to: http://localhost/posttest/test, but it just gives me a 404.

What am I doing wrong?

1

4 Answers 4

1

First of all the routing that Magento uses is in the format http://example.com/route_name/controller/action. So to use your route http://localhost/posttest/test create Index.php inside app/code/Post/Test/Controller/Test folder. I am sharing the action code for get request below.

app/code/Post/Test/Controller/Test/Index.php:

<?php

namespace Post\Test\Controller\Test;

use Magento\Framework\App\Action\HttpGetActionInterface;

class Index implements HttpGetActionInterface
{
    public function execute()
    {
        echo "controller action"; exit;
    }
}

For post request you need to change the controller above and also need to use the csrf token in the form.

1

You are Hitting a Get Request in a Controller implemented with HttpPostActionInterface

try to change the interface from HttpPostActionInterface to HttpGetActionInterface

like this

use Magento\Framework\App\Action\HttpGetActionInterface;

class Index implements HttpGetActionInterface
{
    public function execute()
    {
       //TODO do what you want
    }
}
1

I think this is the pattern that Magento uses :

http://localhost.com/exmapleroute_name/controller/action

So to use your route http://localhost/posttest/test create Index.php inside

app/code/Post/Test/Controller/Test folder.

0

All of these answers helped me understand the organization/structure for controllers but it turns out that my problem was that I was submitting the POST request from Postman, which wasn't including the CSRF token. I would have expected that a forbidden error would have been returned if it was failing CSRF but it was still just giving me a 404.

I ended up writing the class like this:

class Index extends Action implements HttpPostActionInterface

And then submitted the form via my site and it worked.

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.