首页 > 解决方案 > 项目位于“htdocs”文件夹中时未捕获的错误

问题描述

我创建了一个作曲家项目,目的是测试 MongoDB 和 PHP(所有扩展都已加载到项目中)

这是我的示例composer.json项目:

{
    "name": "user/project-name",
    "type": "project",
    "require": {
        "mongodb/mongodb": "^1.4"
    }
}

在根目录中,我创建了一个index.php包含以下内容的文件:

<?php
require_once './vendor/autoload.php';

$manager = new \MongoDB\Driver\Manager('mongodb://localhost:27017');
// Rest of the project comes here. Doesn't matter because this is the point of failure

如果我将项目放置在htdocs文件夹中(使用 XAMPP),我会收到以下错误:

Uncaught Error: Class 'MongoDB\Driver\Manager' not found in 
/Applications/XAMPP/xamppfiles/htdocs/project-name/index.php:4 Stack trace: #0 {main} thrown in 
/Applications/XAMPP/xamppfiles/htdocs/project-name/index.php on line 4

但是,如果我使用 PHP 服务器 ( php -S localhost:8000) 运行它,它可以正常工作:

发生这种情况可能有原因吗?

标签: phpxamppcomposer-phpphp-7

解决方案


ku-chan发现 MacOS High Sierra 带有一个 PHP 版本,当您php从终端运行命令时会使用该版本。这安装了正确的扩展,而 XAMPP 附带的 PHP 版本没有。


推荐阅读