19

I was trying to install a plugin and I removed var/generation and var/cache/*. I've checked the MAGE_MODE and I'm in developer but now when I access the front-end and the admin I get random errors saying mean things like: Class Magento\Customer\Model\CustomerFactory does not exist

Class Magento\Sales\Model\ResourceModel\Report\OrderFactory does not exist

Some of the classes are being created, but not all. I've tried clearing cache again, and the generation folder but then I just get the same errors.

Any ideas?

4
  • 1
    check the rights on the var/generation folder
    – Marius
    Feb 24, 2016 at 16:08
  • it says drwxrwxrw- 5
    – jstrez77
    Feb 24, 2016 at 16:16
  • I had to clean the var/generation folder a few times, go to the home page first, the clean, then admin, then login, then clean... and it worked...?? I don't understand this
    – jstrez77
    Feb 24, 2016 at 17:00
  • Definitely permissions within 'var/generation' folder. Jul 20, 2017 at 15:33

7 Answers 7

26

Magento 2 generates Factory classes inside var/generation directory. So, if there is folder permission issue or folder owner issue with that directory then the factory class cannot be generated and you get such error.

When you clear var/generation directory and set appropriate permission to it then this error should be solved.

See more on Code generation in Magento 2

4
12

For me, i removed var/generation folder then i ran magento setup:di:compile which generated all necessary classes.

(in my case : Then i was encountered with cache issue, i removed initial cache folder.)

Then it solved my issue. Cheers !!

1
  • 1
    needed to change files owner at the end Oct 9, 2016 at 20:45
9

In magento new versions like 2.2.2 and above this code generation path changed to

<Magento root folder>/generated

so you need to check permissions for this folder as well. You need to run following command at the root of Magento installation directory will resolve this issue:

sudo chmod -R 777 generated/
2
1

Magento 2 creates the factory classes in the var/generation folder and if you add a new parameter in your __construct() and it's Factory class does not exists then Magento 2 throws the above error message. Even though there are few solutions give already for you question, i can suggest you another solution which does not require you to clear your generation folder. Please use the below command after any change is made related to Factory classes:

bin/magento setup:di:compile

This command can be more useful on production server.

-1

It's just <magento-root>/var/ folder permission issue.

Execute below command using CLI mode

sudo chmod -R 777 var/
-2

Class ...Factory does not exist means your model class not loaded. As Magento 2 based on modular programming so we need to specify the model class where we want to use explicitly. We can use our model class in two types:

1.by "use"

use Magento\Customer\Setup\CustomerSetupFactory;

public function __construct(CustomerSetupFactory $customerSetupFactory)
{
    $this->customerSetupFactory = $customerSetupFactory;
}
  1. Directly

    public function __construct(Magento\Customer\Setup\CustomerSetupFactory $customerSetupFactory){
    
    $this->customerSetupFactory = $customerSetupFactory;
    
    }
    

When you hit the URL(http://domain.com/your module frontname) of your plugin on browser than the Magento generate the code for your module as below:

\var\generation\Your Vendor Directory\Your Module Directory\Controller
\var\generation\Your Vendor Directory\Your Module Directory\Model

In your case you need to check the "CustomerFactory" and "OrderFactory" is used as above format or not.

-2

Give 777 permission and change ownership to www-data of generated/code folder and then run the command sudo php bin/magento setup:di:compile clear cache and boom your problem is solved

1
  • setting permissions to 777 is a security vulverability
    – Cypher909
    Jun 30, 2020 at 20:29

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.