我想使用 Swiftmailer 作为独立库,而不使用 Symfony、Laravel、Yii、composer 等。
根据Swiftmailer website包含该库的方法是调用 Composer 的 autoload.php,但是在本例中我没有使用 Composer,并且想知道需要包含/vender/swiftmailer 中的哪些基本文件才能使用该库而不是 require_once '/path/to/vendor/autoload.php';
我查看了 Swiftmailer Google Group ,但它已经停产了,并说要到这里来。
谢谢
最佳答案
正如您所发现的,如果您将 Swiftmailer 放入一个没有任何帮助加载的网站中,您将需要为外部依赖项添加您自己的自动加载器。
Swiftmailer v6.1.0 有两个: https://github.com/egulias/EmailValidator 和另一个 https://github.com/doctrine/lexer
包括 swiftlib/swift_required.php 会自动加载所有 swift 类。您可以将其他文件的自动加载添加到同一文件中。下面是一个示例,其中两个依赖项已复制到 swiftlib 文件夹的子目录中:
spl_autoload_register(function ($class) {
$load_deps = [
'Egulias\\EmailValidator\\' => __DIR__ . '/EmailValidator/',
'Doctrine\\Common\\Lexer\\' => __DIR__ . '/Doctrine/Common/Lexer/',
];
foreach ($load_deps as $prefix => $base_dir) {
// does the class use the namespace prefix?
$len = strlen($prefix);
if (strncmp($prefix, $class, $len) == 0) {
// get the relative class name
$relative_class = substr($class, $len);
// replace the namespace prefix with the base directory, replace namespace
// separators with directory separators in the relative class name, append
// with .php
$file = $base_dir . str_replace('\\', '/', $relative_class) . '.php';
// if the file exists, require it
if (file_exists($file)) {
require $file;
}
}
}
});
关于php - 在没有框架的情况下使用 Swiftmailer,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44467212/