2016-12-05 57 views
1

我試圖通過在Symfony 3中的SwiftMailer發送一封電子郵件。我正在閱讀該教程:http://tutorial.symblog.co.uk/docs/validators-and-forms.html#sending-the-email,並且我遇到了「在控制器中創建表單」 : 「試圖調用名爲一個未定義的方法 」「 一流的 」調用getRequest的appbundle \控制器\ DefaultController試圖調用類的名爲「getRequest」的未定義方法。 Sendind電子郵件在SwiftMailer Symfony

這是我的contactAction()中的src /的appbundle/DeffaultController「。」:

/** 
* @Route("/contact"), name="cont") 
*/ 
public function contactAction() 
{ 

    $enquiry = new Enquiry(); 
    $form = $this->createForm(EnquiryType::class, $enquiry); 

    $request = $this->getRequest(); 
    if ($request->getMethod() == 'POST') { 
     $form->bindRequest($request); 

     if ($form->isValid()) { 
      // Perform some action, such as sending an email 

      // Redirect - This is important to prevent users re-posting 
      // the form if they refresh the page 
      return $this->redirect($this->generateUrl('app_default_contact')); 
     } 
    } 


    return $this->render(':default:contact.html.twig', array(
     'form' => $form->createView() 
    )); 
} 

請幫助!

+0

你的文件和類名相同,並且在命名空間設置是否正確?你的課是否擴展了根控制器,讓你可以訪問這些方法? – Psyco430404

回答

3

getRequestSymfony\Bundle\FrameworkBundle\Controller\Controller基類的一種方法。它從版本2.4開始已被棄用,並且已在3.0中刪除。

爲了得到它在你的控制器,只需將其添加作爲參數,類型,暗示其與Request類:

use Symfony\Component\HttpFoundation\Request; 

public function contactAction(Request $request) 
{ 

    // ... 

文檔here

+0

你能告訴我應該在代碼中改變什麼,因爲我真的不知道。 :( – Aga95

+0

你沒有讀過上面的代碼? – Federkun

+0

如何更改該行?$ request = $ this-> getRequest(); – Aga95

0

函數getRequest()在symfony-3及更高版本中被棄用和刪除。的Symfony \捆綁\ FrameworkBundle \控制器\控制器;仍在當前(4.03)版本中使用和維護。 github.com/symfony/framework-bundle

得到請求:

$request = $this->container->get('request_stack')->getCurrentRequest(); 
相關問題