1

I have created new module called 'Vendor/Modulename' in magento2. It is working fine. But i was unable to create a new custom controller and action in the same module.

  • can you paste the code of second controller that is not working ? – Ilja Jan 11 '17 at 6:49
4

Namespace: Prince

Module: Helloworld

Create first index controller and index action

app/code/Prince/Helloworld/Controller/Index/Index.php

<?php

namespace Prince\Helloworld\Controller\Index;

class Index extends \Magento\Framework\App\Action\Action
{
    public function execute()
    {
        echo "This is First Controller";
        exit;
    }
}

Create second - second controller and index action

app/code/Prince/Helloworld/Controller/second/Index.php

<?php

namespace Prince\Helloworld\Controller\Second;

class Index extends \Magento\Framework\App\Action\Action
{
    public function execute()
    {
        echo "This is second controller";
        exit;
    }
}

Now add routes in routes.xml

app/code/Prince/Helloworld/etc/frontend/routes.xml

<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:App/etc/routes.xsd">
    <router id="standard">
        <route id="hello" frontName="hello">
            <module name="Prince_Helloworld" />
        </route>
    </router>
</config>

Now run index controller and index action

http://localhost/magento2/hello/index/index

Now run second controller and index action

http://localhost/magento2/hello/second/index

| improve this answer | |

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