1
1
1alexandr2015-01-09 18:23:58
symfony
1alexandr, 2015-01-09 18:23:58

symfony2. How to address the mailer correctly?

Hello dear experts!
Please tell me what I'm doing wrong (I'm a noob, don't swear). Here is a piece of code:

public function showAction(Request $request)
    {
        $dataMail = [
            'name' => '',
            'subject' => '',
            'email' => '',
            'message' => '',
        ];

        $form = $this->formFactory->create('send_mail_form', $dataMail);
        
        if ($request->getMethod() == 'POST') {
            $form->bind($request);
            $dataMail = $form->getData();

            $message = \Swift_Message::newInstance()
                ->setSubject($dataMail['subject'])
                ->setFrom($dataMail['email'])
                ->setTo('[email protected]')
                ->setBody($this->renderView('AllergoMainBundle:Service:body_mail.html.twig', [
                    'name' => $dataMail['name'],
                    'message' => $dataMail['message'],
                ]));
            $this->get('mailer')->send($message);
        }

        return [
            'form' => $form->createView(),
        ];
    }

The following error pops up, as I understand it because of $this->get('mailer'):
Error: Call to a member function get() on a non-object

I think I figured out how to solve the problem, I need to find the mailer's namespace, I can't find it

Answer the question

In order to leave comments, you need to log in

5 answer(s)
A
Alex, 2015-01-09
@shoomyst

Everything should work if the class inherits from the symphony controller

S
Sergey, 2015-01-09
Protko @Fesor

In addition to shoomyst 's answer , if you don't want to inherit from Symphony's controller, implement the ContainerAwareInterface interface for your controller and fetch from the container yourself. You can also connect a trait.

class MyController implements ContainerAwareInterface
{
    use \Symfony\Component\DependencyInjection\ContainerAwareTrait;

    function myAction(Request $request) {
        // ...
        $this->container->get('mailer')->send($message);
    }
}

#edited
Or, as OnYourLips advises , if you are already making controllers as services, then do them correctly, indicating all the dependencies in the constructor. True, I think this approach is superfluous, it's better to just keep thin controllers that simply pass data to the service for processing, and our business logic does not depend on HttpFoundation and is minimally tied to the framework.

O
OnYourLips, 2015-01-09
@OnYourLips

Use Dependency Injection.
Get an object of the Mailer class in the constructor.

Q
Quber, 2015-01-09
@Quber

You forgot at the beginning of the file, where the class name is to indicate inheritance from the symphonic class extends Controller
It is done like this

class YourController extends Controller
{
    public function showAction(Request $request) {
        // your actions
    }
}

This will allow you to call mailer. But as noted above, there is another approach related to ContainerAwareInterface. But I think it's redundant. Do as I have and that's it.

I
Ivan, 2015-01-10
@0neS

You don't need a mailer's namespace if you call it as a service via get('mailer') .
Make sure you inherit the controller from Symfony\Bundle\FrameworkBundle\Controller\Controller ;

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question