'Attempted to call an undefined method named "get" of class "MailController"

I want to sent an email with swiftmailer this is my function in my mailController.php

public function newMail(Mail $mail){
        $message = \Swift_Message::newInstance()
            ->setSubject('Accusé de réception')
            ->setFrom('[email protected]')
            ->setTo($mail->getEmail())
            ;

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

error : Attempted to call an undefined method named "get" of class "UserBundle\Controller\MailController".



Solution 1:[1]

Add the use statement atop your controller class and then modify your controller to extend it:

use Symfony\Bundle\FrameworkBundle\Controller\Controller;

class YourController extends Controller
{
  // ...
}

That's it! You now have access to methods like $this->get() or $this->render() and many others.

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 DonCallisto