Silex traits for swiftmailer. Fatal error: Call to undefined method Silex\Application::mail()

Silex traits for swiftmailer. Fatal error: Call to undefined method Silex\Application::mail()

我正在尝试将 Silex 中的特征用于 Swift 邮件程序。

我已经包括:

use Silex\Application\SwiftmailerTrait;

我还检查了 traits 文件是否位于正确的 Silex 供应商目录中。

测试特征:

$app->mail(\Swift_Message::newInstance()
    ->setSubject("title")
    ->setFrom(["www.domain.com"]])
    ->setTo(["something@domain.com"])
    ->setReplyTo(["user.email@some.com"])
    ->setBody("TEST MESSAGE")
);

然后,我收到此错误消息:

Fatal error: Call to undefined method Silex\Application::mail() in ...\app.php on line 88

只是为了说清楚。我可以毫无问题地使用在 Silex 中使用 swift 的标准方法,它工作得很好。

这是没有特征的工作位:

    // $message = \Swift_Message::newInstance()
    // ->setSubject('[YourSite] Feedback')
    // ->setFrom(array('noreply@yoursite.com'))
    // ->setTo(array('feedback@yoursite.com'))
    // ->setBody($request->get('message'));
    // $app['mailer']->send($message);

但是我想知道实际上是什么阻止了 Silex 运行 swift 的特性。有什么想法吗?

我正在使用 PHP 版本 5.6.11 我的作曲家文件:

{
    "require": {
        "components/jquery": "^2.2",
        "components/css-reset": "^2.5",
        "silex/silex": "~1.2",
        "symfony/browser-kit": "~2.3",
        "symfony/console": "~2.3",
        "symfony/config": "~2.3",
        "symfony/css-selector": "~2.3",
        "symfony/dom-crawler": "~2.3",
        "symfony/filesystem": "~2.3",
        "symfony/finder": "~2.3",
        "symfony/form": "~2.3",
        "symfony/locale": "~2.3",
        "symfony/process": "~2.3",
        "symfony/security": "~2.3",
        "symfony/serializer": "~2.3",
        "symfony/translation": "~2.3",
        "symfony/validator": "~2.3",
        "symfony/monolog-bridge": "~2.3",
        "symfony/twig-bridge": "~2.3",
        "doctrine/dbal": ">=2.2.0,<2.4.0-dev",
        "swiftmailer/swiftmailer": "5.*",
        "twig/twig": "^1.24",
        "symfony/security-csrf": "~2.3",
        "symfony/yaml": "~2.3"
    },
    "autoload": {
        "psr-4": {
            "WL\Form\": "WL/Form/",
            "WL\Email\": "WL/Email/"
        },
        "classmap":[],
        "files":[]
    }
}

您需要创建一个自定义 Application class 来扩展 \Silex\Application 并使用该特征。

假设基础项目树为:

project/
  |
  |_app/
  |
  |_src/
  |
  |_vendor/
  |
  |_web/

您需要一个 class 定义:

// src/WL/App.php

namespace WL;

class App extends \Silex\Application
{
    use \Silex\Application\SwiftmailerTrait;

    // add some other trait
    // even custom methods or traits
}

A bootstrap :

// app/bootstrap.php

$app = new \WL\App();

// configure it, register controllers and services, ...

// or import them
foreach (glob(__DIR__ . "/../src/WL/Controller/*.php") as $controllers_provider) {
    include_once $controllers_provider;
}

return $app;

因此您可以导入控制器集合,例如:

// src/Wl/Controller/blog.php

use Symfony\Component\HttpFoundation\Request;

/** @var \Silex\ControllerCollection $blog */
$blog = $app['controllers_factory'];

// define some routes

$blog->post('/send-mail', function (Request $request, \WL\App $app)
{
    // Now this application passed to your controller is an
    // instance of custom \App which has the trait you want
    // in contrary with the default \Silex\Application

    $app->mail(...

}

$app->mount('/blog', $blog);

还有一个前端控制器:

// web/index.php

// define autoloading
// customize debug and server parameters

$app = require_once '../app/bootstrap.php';

$app->run();