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?

Amit Bera
  • 77,456
  • 20
  • 123
  • 237
jstrez77
  • 772
  • 1
  • 10
  • 24

7 Answers7

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

Mukesh Chapagain
  • 5,383
  • 4
  • 37
  • 51
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 !!

Suman K.C
  • 1,159
  • 14
  • 31
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/
Nadeem0035
  • 1,227
  • 12
  • 19
Hassan Ali Shahzad
  • 2,330
  • 18
  • 35
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/
Softec
  • 2,065
  • 2
  • 11
  • 31
Anwar
  • 75
  • 1
  • 13
-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.

Suman Singh
  • 1,007
  • 2
  • 10
  • 22
-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