12

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?

  • 1
    check the rights on the var/generation folder – Marius Feb 24 '16 at 16:08
  • it says drwxrwxrw- 5 – jstrez77 Feb 24 '16 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 '16 at 17:00
19

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

8

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
    needed to change files owner at the end – moshe beeri Oct 9 '16 at 20:45
1

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/
0

Definitely permissions within 'var/generation' folder.

0

It's just var folder permission issue. in CLI mode follow the following command

sudo chmod -R 777 var/

0

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.

0

The following commands are needed to be executed:

sudo php -f bin/magento setup:di:compile 

sudo rm -rf pub/static/*/*; sudo rm -rf var/*/*; 
sudo php -f bin/magento setup:static-content:deploy -f  

sudo php bin/magento cache:clean

Also, proper permission is needed. If on production, you need to execute proper permissions. But, on local machine, you can run full permission:

sudo chmod -R 777 *
-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.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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