Yii2中PhpOffice库识别异常求助:同仓库两台电脑表现不一致
Hey there, let's break down why your second machine is hitting this error even though the vendor library exists and your composer files match the working machine. The key difference here is how the library was installed—Yii's Composer integration vs manual vendor addition—so we'll focus on closing that gap.
1. Regenerate Composer Autoload Files
When you manually copy the vendor folder, you skip the critical step where Composer generates its autoload mappings. Yii's Composer mechanism handles this automatically, but manual copies don't. Run this command in your project root to fix it:
composer dump-autoload
This refreshes the vendor/autoload.php file and class maps, ensuring PHP can locate the PhpSpreadsheet classes correctly.
2. Verify Yii's Autoloader Configuration
Yii has its own autoload setup that hooks into Composer's autoloader. Check if your second machine's Yii config is properly referencing it:
- Open your main config file (like
config/web.phporconfig/console.php) - Ensure the
autoloadercomponent (if configured) isn't overriding or missing the Composer autoload include. - Double-check that
Yii.php(invendor/yiisoft/yii2/) correctly includesvendor/autoload.phpat the top. It should look like this:require(__DIR__ . '/../../autoload.php');
3. Check Permissions and Case Sensitivity
- Permissions: Make sure your web server or CLI user has read access to the
vendor/phpoffice/phpspreadsheetdirectory. On Linux/macOS, run:chmod -R 755 vendor/phpoffice/ - Case Sensitivity: If your first machine is Windows (case-insensitive) and the second is Linux/macOS (case-sensitive), double-check the class path spelling. For example, confirm it's
PhpOffice\PhpSpreadsheet\Reader\Xlsx(note the capitalSinPhpSpreadsheet—typos here will break things on case-sensitive systems).
4. Clear Yii and PHP Caches
Cached class mappings can keep old, missing references active:
- Yii Cache: Delete all files in your project's
runtime/cachedirectory. - OPcache: If your PHP setup uses OPcache, restart your PHP service (Apache/Nginx/FPM) to clear cached class data. You can also temporarily disable OPcache in php.ini to test if this is the root cause.
5. Validate Vendor Directory Integrity
Even if the folder exists, manual copies might miss files or have corrupted data. Compare the vendor/phpoffice/phpspreadsheet/src/Reader/Xlsx.php file between both machines—ensure it exists and has the correct content. If anything's off, run composer install on the second machine (since your composer.json and lock are identical) to re-install the library properly instead of relying on manual copies.
内容的提问来源于stack exchange,提问作者Zoe

