如何访问供应商目录中的包

How to access packages in vendor directory

从任何 CakePHP 控制器中的 vendor 目录结构调用任何包都可以,因为 composer 正确设置了所有内容。例如来自 https://github.com/giggsey/libphonenumber-for-php#quick-examples

的这个 MCVE
$swissNumberStr = "044 668 18 00";
$phoneUtil = \libphonenumber\PhoneNumberUtil::getInstance();
try {
    $swissNumberProto = $phoneUtil->parse($swissNumberStr, "CH");
    var_dump($swissNumberProto);
} catch (\libphonenumber\NumberParseException $e) {
    var_dump($e);
}

当我 运行 直接在 webroot/sample.php 中使用相等代码时,它失败了:

Got error 'PHP message: PHP Fatal error: Uncaught Error: Class 'libphonenumber\PhoneNumberUtil' not found in ...sample.php

我的问题:

我必须在 CakePHP world 之外做什么才能使用 vendor 目录结构中的包?

我环顾四周,在 vendor 目录中看到文件 autoload.php,我试了一下。

解决方法是:

在 CakePHP 之外 world 我只需要包括这个 autoload.php:

include( __DIR__ . '/../vendor/autoload.php');

然后 vendor 目录中的所有包都可用。