【发布时间】:2014-06-06 10:07:18
【问题描述】:
我在尝试将 sendgrid 库集成到 Symfony2 时遇到问题。我们已经将库复制到我们的包中,并将其包含在服务中。如果此服务被某个操作调用,则说明它运行的库成功。
这是服务:
use Symfony\Component\DependencyInjection\ContainerInterface;
require_once(__DIR__.'/SendgridPhp/sendgrid-php.php');
class MailerService extends \Twig_Extension {
protected $container;
private $mailer;
private $templating;
public function __construct(ContainerInterface $container,$mailer,$templating)
{
$this->container= $container;
$this->mailer = $mailer;
$this->templating=$templating;
}
public function sendEmail($to, $from, $subject, $body, $attachment = null)
{
$sendgrid = new \SendGrid($this->container->getParameter('sendgrid_user'), $this->container->getParameter('sendgrid_password'));
$email = new \SendGrid\Email();
$email->setFrom($from)
->setFromName('Name')
->setSubject($subject)
->addTo($to)
->setHtml($body, 'text/html');
$salida = $sendgrid->send($email );
}
}
当我们从 symfony2 命令调用此服务时,就会出现问题。
$mailerService = $container->get('mailer.service');
$mailerService->sendEmail($user->getEmail(), $container->getParameter("sender_email"), 'Message', $body);
命名空间中的错误是下一个:
PHP Fatal error: Class 'SendGrid\Email' not found in /var/www/SpainStartup/src/SpainStartup/CommunicationBundle/Services/MailerService.php on line 35
我们应该做一些特殊的事情来在命令上下文中加载库吗?
提前致谢
【问题讨论】:
-
“我们已经复制了包中的库” - 听起来很可疑。通过您的作曲家配置提供它作为依赖项,然后自动加载应该可用。 (这很常见,在您的 Bundle = 只有您的代码,即您编写的代码)。