I'm working on a module which will modify the URLs for our category pages using a Magento 2 preference. I've created a module which looks correct, but on most pages the url is unchanged and on product pages it refuses to load, with apache reporting the error:
PHP Fatal error: Class 'Company\\CategoryUrls\\Model\\Category' not found in [root folder]/vendor/magento/framework/ObjectManager/Factory/AbstractFactory.php on line 93
As far as I can see the module should be correct, and I know the change in behaviour works as I've tried editing it directly into the file in the vendors folder,but I just can't seem to get it to work. If anyone's got any suggestions it'd be appreciated
Module File:
<?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="Company_CategoryUrls" setup_version="1.0.0" ></module>
</config>
DI file:
<?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\Catalog\Model\Category" type="Company\CategoryUrls\Model\Category"/>
</config>
Class:
<?php
namespace Company\CategoryUrls\Model;
use \Magento\Catalog\Model\Category;
class CategoryUrl extends Category{
/**
* Get category url
*
* @return string
*/
public function getUrl()
{
[contents of parent class]
}
}
Best Answer
Your preference file is expecting a called
Company\CategoryUrls\Model\Category
however your class isCompany\CategoryUrls\Model\CategoryUrl
.You can either update your preference
Or change your class name to be
Category