2

I'm trying to extend Magento 2 default module Magente_Contact, but I can't change the frontName, I tried to put in the di.xml, but nothing happens.

<config>
    <router id="standart">
        <route id="contact" frontName="contato">
            <module name="[VENDOR]_Contact" />
        </route>
    </router>
</config>


<?xml version="1.0"?>
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:ObjectManager/etc/config.xsd">
  <preference for="Magento\Contact\etc\frontend\routes" type="{Vendor}\{Module}\etc\front\routes" />
</config>

2 Answers 2

3

There is a tricky way,you can change the contact us Magento_Contact module's frontend Name

First, etc/module.xml of your module's make Magento_Contact as a depended module.

<?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="[Vendor]_[ModuleName]" setup_version="1.0.0">
        <sequence>
            <module name="Magento_Contact"/>
        </sequence>
    </module>
</config>

And then changes frontend name from routes.xml at etc/frontend/.

<?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="contact" frontName="contato">
            <module name="Magento_Contact" />
        </route>
    </router>
</config>

di.xml is not required

5
  • Thanks for the answer, but that didn't work, error: Front controller reached 100 router match iterations. And if I try to access {site_url}/contact, It still works. I changed Magente_Contact to Magento_Contact. Isn't necessary a di.xml file? Commented Jul 5, 2018 at 11:20
  • It looks like a can't use route id as contact <route id="contact" frontName="contato"> , but if I change it can't find the right layout file. Commented Jul 5, 2018 at 11:47
  • di.xml is not requied
    – Amit Bera
    Commented Jul 5, 2018 at 12:01
  • aha. od good bad mistake changed Magente_Contact to Magento_Contact..Updated answer
    – Amit Bera
    Commented Jul 5, 2018 at 12:02
  • It still doesn't work, with Magento_Contact. Keep happening Front controller reached 100 router match iterations if I use router id as "contact". Commented Jul 5, 2018 at 12:30
0

So, I couldn't rewrite the route frontName. What i did to work around it, was create a new frontName route with a new Controller and rewrite like this:

use Magento\Framework\Controller\ResultFactory;
use Magento\Contact\Model\ConfigInterface;
use Magento\Framework\App\Action\Context;
use Magento\Framework\App\Request\Http;

class Index extends \Magento\Contact\Controller\Index\Index
{
    protected $_http;

    protected $resultFactory;

    public function __construct(Context $context, ConfigInterface $contactsConfig, ResultFactory $resultFactory, Http $http)
    {
        parent::__construct($context, $contactsConfig);
        $this->_http = $http;
        $this->resultFactory = $resultFactory;
    }

    public function execute()
    {
        $this->_http->setRouteName('contact');
        $resultPage = $this->resultFactory->create(ResultFactory::TYPE_PAGE);
        $resultPage->getConfig()->getTitle()->set(__("Contato"));
        return $resultPage;
    }
}

Not the best solution, but worked.

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.