我正试图利用Silex中的特性作为Swift mailer。
我已包括:
use Silex\Application\SwiftmailerTrait;我还检查了属性文件在正确的Silex供应商目录中。
测试性状:
$app->mail(\Swift_Message::newInstance()
->setSubject("title")
->setFrom(["www.domain.com"]])
->setTo(["something@domain.com"])
->setReplyTo(["user.email@some.com"])
->setBody("TEST MESSAGE")
);然后,我得到以下错误消息:
致命错误:调用第88行...\app.php中的未定义方法Silex\Application::mail()
只是想说清楚。我可以,没有任何问题,使用标准的方式使用斯威夫特在Silex和它只是很好的工作。
这是一个没有特征的工作点:
// $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跑得飞快而有特色。知道吗?
我正在使用PHPVersion5.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":[]
}
}发布于 2016-03-06 15:00:17
您需要创建一个自定义的Application类,它扩展了\Silex\Application并使用了这个特性。
假设基础项目树为:
project/
|
|_app/
|
|_src/
|
|_vendor/
|
|_web/您需要一个类定义:
// src/WL/App.php
namespace WL;
class App extends \Silex\Application
{
use \Silex\Application\SwiftmailerTrait;
// add some other trait
// even custom methods or traits
}鞋带:
// 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();https://stackoverflow.com/questions/35788335
复制相似问题