Attempted to call an undefined method named “getRequest” of class . Sendind e-mail in SwiftMailer Symfony

☆樱花仙子☆ 提交于 2019-12-24 01:16:08

问题


I'm trying to sen an e-mail by SwiftMailer in Symfony 3. I'm going through that tutorial: http://tutorial.symblog.co.uk/docs/validators-and-forms.html#sending-the-email and I've got the problem in "Creating the form in the controller" : "Attempted to call an undefined method named "getRequest" of class "AppBundle\Controller\DefaultController". "

That's my contactAction() in 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()
    ));
}

Help please!


回答1:


getRequest was a method of the Symfony\Bundle\FrameworkBundle\Controller\Controller base class. It was deprecated since version 2.4 and it's been removed in 3.0.

To get it in your controller, just add it as an argument and type-hint it with the Request class:

use Symfony\Component\HttpFoundation\Request;

public function contactAction(Request $request)
{

    // ...

Documentation here.




回答2:


the function getRequest() is deprecated and removed after symfony-3 and later. Symfony\Bundle\FrameworkBundle\Controller\Controller; is still used and maintained in current (4.03) version. github.com/symfony/framework-bundle

to get the request:

$request = $this->container->get('request_stack')->getCurrentRequest();



回答3:


instead of getRequest use $request....



来源:https://stackoverflow.com/questions/40981191/attempted-to-call-an-undefined-method-named-getrequest-of-class-sendind-e-ma

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!