2016-03-04 50 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(); 
相關問題