1

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]
}
}
2
  • run php bin/magento setup:di:compile command it will generate factory class again and then check. might it will help you Aug 2, 2017 at 9:10
  • Unfortunately I'd made a mistake when renaming one of the classes and missed an instance of it, causing it not to find the class. Usually what you suggested does work though
    – SolidSquid
    Aug 2, 2017 at 15:57

1 Answer 1

0

Your preference file is expecting a called Company\CategoryUrls\Model\Category however your class is Company\CategoryUrls\Model\CategoryUrl.

You can either update your preference

<?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\CategoryUrl"/>
 </config>

Or change your class name to be Category

1
  • Can't believe I missed that, changed it previously to avoid a conflict but apparently missed one of the uses
    – SolidSquid
    Aug 2, 2017 at 15:56

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.