Magento 2 Error – Fatal Error: Uncaught Error: Class ‘Cli’ Not Found

magento2PHP

Someone changed my php version for my magento 2 website to 5.6 and I changed it back to 7.0 and now I'm getting this error when I try to run the major magento commands such as compile and system:upgrade

The file "/var/www/vhosts/ooples.com/httpdocs/var/generation/Composer/Console/ApplicationFactory.php"
cannot be deleted
Warning!unlink(/var/www/vhosts/ooples.com/httpdocs/var/generation/Composer/Console/ApplicationFactory.php):
Permission denied#0
/var/www/vhosts/ooples.com/httpdocs/lib/internal/Magento/Framework/Filesystem/Driver/File.php(405):
Magento\Framework\Filesystem\Driver\File->deleteFile('/var/www/vhosts…')
1 /var/www/vhosts/ooples.com/httpdocs/lib/internal/Magento/Framework/Filesystem/Driver/File.php(403):
Magento\Framework\Filesystem\Driver\File->deleteDirectory('/var/www/vhosts…')
2 /var/www/vhosts/ooples.com/httpdocs/lib/internal/Magento/Framework/Filesystem/Driver/File.php(403):
Magento\Framework\Filesystem\Driver\File->deleteDirectory('/var/www/vhosts…')
3 /var/www/vhosts/ooples.com/httpdocs/setup/src/Magento/Setup/Console/CompilerPreparation.php(68):
Magento\Framework\Filesystem\Driver\File->deleteDirectory('/var/www/vhosts…')
4 /var/www/vhosts/ooples.com/httpdocs/lib/internal/Magento/Framework/Console/Cli.php(74):
Magento\Setup\Console\CompilerPreparation->handleCompilerEnvironment()
5 /var/www/vhosts/ooples.com/httpdocs/bin/magento(22): Magento\Framework\Console\Cli->__construct('Magento CLI')
6 {main}

PHP Fatal error: Uncaught Error: Class 'Cli' not found in
/var/www/vhosts/ooples.com/httpdocs/bin/magento:31 Stack trace:
0 {main} thrown in /var/www/vhosts/ooples.com/httpdocs/bin/magento on line 31

Fatal error: Uncaught Error: Class 'Cli' not found in
/var/www/vhosts/ooples.com/httpdocs/bin/magento:31 Stack trace:
0 {main} thrown in /var/www/vhosts/ooples.com/httpdocs/bin/magento on line 31

What can I do to fix this error?

UPDATE: Here are the results when I run rm -rf var

rm: cannot remove ‘var/di/setup.ser’: Permission denied
rm: cannot remove ‘var/di/webapi_rest.ser’: Permission denied
rm: cannot remove ‘var/di/frontend.ser’: Permission denied
rm: cannot remove ‘var/di/global.ser’: Permission denied
rm: cannot remove ‘var/di/adminhtml.ser’: Permission denied
rm: cannot remove ‘var/di/crontab.ser’: Permission denied
rm: cannot remove ‘var/di/webapi_soap.ser’: Permission denied

Best Answer

This error has nothing to do with Plesk it's from the Magento script itself. Whoever wrote it clearly has no idea of PHP OOP. Sorry but it's true.

There's nowhere to be found an "use" keyword in the script but you can clearly see that he's trying to use

  Cli

class without specifying the namespace. That's the whole error if you change it to

  Magento\Framework\Console\Cli

it will work.

Change the bin/magento shell file from:

#!/usr/bin/env php
<?php
/**
 * Copyright © 2013-2017 Magento, Inc. All rights reserved.
 * See COPYING.txt for license details.
 */

if (PHP_SAPI !== 'cli') {
    echo 'bin/magento must be run as a CLI application';
    exit(1);
}

try {
    require __DIR__ . '/../app/bootstrap.php';
} catch (\Exception $e) {
    echo 'Autoload error: ' . $e->getMessage();
    exit(1);
}
try {
    $handler = new \Magento\Framework\App\ErrorHandler();
    set_error_handler([$handler, 'handler']);
    $application = new Magento\Framework\Console\Cli('Magento CLI');
    $application->run();
} catch (\Exception $e) {
    while ($e) {
        echo $e->getMessage();
        echo $e->getTraceAsString();
        echo "\n\n";
        $e = $e->getPrevious();
    }
    exit(Cli::RETURN_FAILURE);
}

to

#!/usr/bin/env php
<?php
/**
 * Copyright © 2013-2017 Magento, Inc. All rights reserved.
 * See COPYING.txt for license details.
 */

if (PHP_SAPI !== 'cli') {
    echo 'bin/magento must be run as a CLI application';
    exit(1);
}

try {
    require __DIR__ . '/../app/bootstrap.php';
} catch (\Exception $e) {
    echo 'Autoload error: ' . $e->getMessage();
    exit(1);
}
try {
    $handler = new \Magento\Framework\App\ErrorHandler();
    set_error_handler([$handler, 'handler']);
    $application = new Magento\Framework\Console\Cli('Magento CLI');
    $application->run();
} catch (\Exception $e) {
    while ($e) {
        echo $e->getMessage();
        echo $e->getTraceAsString();
        echo "\n\n";
        $e = $e->getPrevious();
    }
    exit(\Magento\Framework\Console\Cli::RETURN_FAILURE);
}

Take a look at the exit at the end...

Sorry if I offended somebody ;]

Related Topic