我是一个使用composer的新手,我在安装后的公共php页面中遇到了一些麻烦,包括Swiftmailer文件。我安装了Swiftmailer与作曲家使用以下说明:
php composer.phar require swiftmailer/swiftmailer @stable
所有工作正常,文件安装在
./vendor/swiftmailer/swiftmailer/
由于文件在根目录中,而不是在public_html中,因此我无法包含它们以开始使用它。
我做了一个错误安装Swiftmailer在那个位置吗?我需要换吗?如果是这样,有什么捷径吗?
简而言之:在应用程序引导中添加以下行require 'vendor/autoload.php';
(除非它已经存在)。
你可以简单地包含这个文件,你将获得免费的自动加载。引用:https://getcomposer.org/doc/01-basic-usage.md半自动的
长版:
Swiftermailer将lib/swift_required.php
文件添加到Composer自动加载器:
看到https://github.com/swiftmailer/swiftmailer/blob/master/composer.json L25
这个文件需要一些其他的Swiftmailer文件并且注册Swiftmailer自动加载器
执行流程应该是这样的:
- 您的应用程序index.php 你bootstrap.php
- 要求作曲器自动加载器
- 文件在每次请求时加载,如
swiftmailer_required.php
现在你可以使用
autoload
节中的$mailer = new 'Swift_Mailer();
如果你想使用由Composer安装的东西,你必须在你的代码中包含自动加载器。
因为包含PHP代码使用的是文件系统,而不是web服务器访问,所以就能够通过web服务器访问它而言,脚本位于哪里并不重要-这是一件好事,因为您不希望每个PHP脚本都通过web访问。
你必须在脚本的第一行中包含Composer自动加载器,然后才能使用你在Composer中安装的所有内容。
include 'vendor/autoload.php'; // adjust the path depending on where your script is located.
这可能会导致如下的结果:
./public_html/index.php:
include '../vendor/autoload.php'; // located in ./vendor/
$mailer = new 'Swift_Mailer();