2012-04-05 45 views
0

我想在Symfony2中使用SwiftMailer。 首先,我創建了一個新的控制器,並從http://symfony.com/doc/current/cookbook/email/email.html複製了樣本。 當我嘗試執行renderView時,出現以下錯誤消息:Fatal error: Call to a member function get() on a non-object in var/www/project/Symfony/vendor/symfony/src/Symfony/Bundle/FrameworkBundle/Controller/Controller.php on line 84當使用Symfony2和SwiftMail時,renderView失敗

我嘗試了各種各樣的事情,但他們總是會在控制器中出現錯誤。

不僅renderView而且$this->get('mailer')->send($message);正在引發錯誤。

回答

3

你不能只是實例化一個控制器作爲通常的類,然後調用它的方法,因爲你不然後注入服務容器。改爲創建郵件服務。詳情請閱讀Service Container章節。

0

試試這個:更改順序,創造新的swift_message如此前獲得mailer

$this->get('mailer')->send($message); 

$message = \Swift_Message::newInstance() 
+0

對不起,但它也失敗了。錯誤現在在Symfony/Bundle/FrameworkBundle/Controller/Controller.php中的第189行而不是84. – redshark1802 2012-04-05 14:40:30

+0

可以請提供您這次得到什麼錯誤? – seferov 2012-04-05 15:09:12

+0

'致命錯誤:調用成員函數get()在/var/www/project/Symfony/vendor/symfony/src/Symfony/Bundle/FrameworkBundle/Controller/Controller.php上的一個非對象的第189行' – redshark1802 2012-04-05 15:24:10

相關問題