5

Hello guys how are you?,

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

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

Any suggestion?

8

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.

  • 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 '16 at 3:11
  • @Amasty, I want to add new custom tab for customer account.Could you please guide how I can do this? – mageDev0688 May 15 '17 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 – Cristiano Casciotti Jul 12 '17 at 10:04

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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