aga
aga

Reputation: 3883

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

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!

Upvotes: 2

Views: 7472

Answers (3)

reinerGeist
reinerGeist

Reputation: 13

instead of getRequest use $request....

Upvotes: 0

Andre
Andre

Reputation: 162

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();

Upvotes: 1

Federkun
Federkun

Reputation: 36944

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.

Upvotes: 8

Related Questions