6

Here is my scenario, I want to add a new Action to an existing Magento route, for example under the MyAccount section you can find /sales/order/[view, history, etc]; I want to add an index like an Order section dashboard.

I tried to implement this solution: https://stackoverflow.com/questions/34908125/magento-2-controller-post-throwing-404-error without luck.

Any suggestions?

1 Answer 1

10

So, you need to create a custom extension with the following file structure (for example adding new action to checkout/cart/):

Add action extension structure.

We need the following files to make it work:

1) registration.php

<?php
\Magento\Framework\Component\ComponentRegistrar::register(
    \Magento\Framework\Component\ComponentRegistrar::MODULE,
    'Amasty_Checkout',
    __DIR__
);

2) composer.json

{
    "name": "amasty/checkout",
    "description": "Amasty Test",
    "require": {
        "php": "~5.5.0|~5.6.0|~7.0.0",
        "amasty/base": "*"
    },
    "type": "magento2-module",
    "version": "1.0.0",
    "license": [
        "Commercial"
    ],
    "autoload": {
        "files": [ "registration.php" ],
        "psr-4": {
            "Amasty\\Checkout\\": ""
        }
    }
}

3) 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="Amasty_Checkout" schema_version="1.0.0" setup_version="1.0.0">
    <sequence>
        <module name="Magento_Checkout"/>
    </sequence>
</module>

4) 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">
            <module name="Amasty_Checkout" />
        </route>
    </router>
</config>

5) Test.php (adding /test/ action)

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

class Test extends \Magento\Checkout\Controller\Cart\Add
{

    public function execute()
    {
        die('asdasdasd');
    }
}

And this is the result:

Test action result.

3
  • thank you so much for the answer. I didn't have time to check the fix yet. I'll let you now how was
    – AleGrinGo
    Jun 23, 2016 at 3:11
  • @Amasty, I want to add new custom tab for customer account.Could you please guide how I can do this? May 15, 2017 at 11:13
  • @Amasty in routes.xml file you have to specify only id attribute of route node, and you are strongly advised to use a before or after tag's attribute to control the order Magento will check for matches in, to avoid accidental overrides of existing core controllers Jul 12, 2017 at 10:04

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.