2016-03-04 43 views
2

我想在Silex中使用Swift邮件特征。swilemailer的Silex特征。致命错误:调用未定义的方法Silex Application :: mail()

我已经包括:

use Silex\Application\SwiftmailerTrait; 

我还检查了特征文件是在正确的Silex的供应商目录。

测试性状:

$app->mail(\Swift_Message::newInstance() 
    ->setSubject("title") 
    ->setFrom(["www.domain.com"]]) 
    ->setTo(["[email protected]"]) 
    ->setReplyTo(["[email protected]"]) 
    ->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('[email protected]')) 
    // ->setTo(array('[email protected]')) 
    // ->setBody($request->get('message')); 
    // $app['mailer']->send($message); 

但是我想知道什么实际运行与迅速停止特质捷希凯。任何想法 ?

我使用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":[] 
    } 
} 
+0

的硅石和版本的PHP的哪个版本? – mTorres

+0

我将添加关于我的Silex应用程序正在使用的PHP版本和组件的detials。 – DevWL

回答

2

您需要创建一个自定义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(); 
相关问题