Fatal error - cannot redeclare class Magento 2

We were writing our new module Canonical URL in Magento 2 which allows you to set your own canonical tag on product, category and CMS pages but while we were building this module we came across the following fatal error related to cannot redeclare class -:

Fatal error: Cannot redeclare class ScommerceCanonicalModelAttributesSourceCategories in appcodeScommerceCanonicalModelAttributeSourceCategories.php on line 87

It took our development team a while to figure it out what’s wrong so we thought we should share with the community to save their time.

It was the WRONG NAMESPACE which caused this fatal error. You can laugh now ???? but you need to agree the error itself is so misleading “Cannot redeclare class”

Magento was expecting the following namespace -:

ScommerceCanonicalModelAttributeSourceCategories

But we declared it with ‘S’ in Attributes whereas the folder was Attribute, so it was like this -:

ScommerceCanonicalModelAttributesSourceCategories

That’s it, Hope this article helped you in some way. Please leave us your comment and let us know what do you think? Thanks.

Similar Posts