Magento 2 Upgrade Error – How to Fix Code Generation Issues

code generationerrormagento-2.0magento2upgrade

After firing this command in CLI php bin/magento setup:upgrade

I am getting below error -:

Fatal error: Uncaught exception 'Magento\Framework\Exception\LocalizedException' with message 'Can't create directory 
/var/www/html/magento/magento2.0/var/generation/Magento/Framework/App/ResourceConnection/.' in 
/var/www/html/magento/magento2.0/vendor/magento/framework/Code/Generator.php:103
Stack trace:
#0 /var/www/html/magento/magento2.0/vendor/magento/framework/Code/Generator/Autoloader.php(35): Magento\Framework\Code\Generator->generateClass('Magento\\Framewo...')
#1 [internal function]: Magento\Framework\Code\Generator\Autoloader->load('Magento\\Framewo...') #2 [internal function]: spl_autoload_call('Magento\\Framewo...')
#3 /var/www/html/magento/magento2.0/vendor/magento/framework/Code/Reader/ClassReader.php(19): ReflectionClass->__construct('Magento\\Framewo...')
#4 /var/www/html/magento/magento2.0/vendor/magento/framework/ObjectManager/Definition/Runtime.php(44): Magento\Framework\Code\Reader\ClassReader->getConstructor('Magento\\Framewo...')
#5 /var/www/html/magento/magento2.0/vendor/magento/framework/ObjectMana in /var/www/html/magento/magento2.0/vendor/magento/framework/Code/Generator.php on line 103`

Best Answer

I have solved this problem by just simply doing this steps. follow steps :

  1. Delete the "di" folder located in var directory (for 2.0 and 2.1)
  2. Run this command and go to your project root folder command:

    cd /yourmagentoroot/
    

    If your Magento mode is developer then you don't need to run below commands just remove var/generation directory(2.0 & 2.1) or generated folder(2.2 onwards)

  3. Then run this command

    php bin/magento setup:di:compile
    
  4. Then run this command (required in production mode only)

    php bin/magento setup:static-content:deploy
    
  5. Finally, give file/directory permissions.(If needed)

Related Topic