Is there a way to override model class with custom model class only when going to custom url? Example:When going to


uses core model on generation. Now I want when going to my custom route


rewrite core model with di.xml

<?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\Sitemap\Model\Sitemap" type="Product\Sitemap\Model\Sitemap"/>

Assuming that you would be injecting your class , Product\Sitemap\Model\Sitemap, to your Custom Controller's __construct method,

I would suggest, just extend your Custom Sitemap class to the Core Sitemap class.

Assuming that you have a function or method which you wanted to customize from the 'Core' Sitemap, declare the method in your custom sitemap, make the necessary customization and return the required response.

Let me know how it goes.


When extending Adminhtml controller generate action class execute method, change model load to your desired model. In my case it was:

$sitemap = $this->_objectManager->create(\Product\Sitemap\Model\Sitemap::class);
    /* @var $sitemap \Product\Sitemap\Model\Sitemap */

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.