\ Swift_Mailer期待变量

时间:2017-08-01 21:59:25

标签: php swiftmailer symfony-3.2

在这个question:有人说:

  

只需从构造函数中删除$ mailer(您不在服务中,您在控制器中),并使用$ this-> get('mailer') - > send($ message);

我这样做了,但现在我收到了这个错误:

  

解析错误:语法错误,意外',',期待变量(T_VARIABLE)(第20行)

这是我的代码

<?php

namespace AppBundle\Controller;

use Sensio\Bundle\FrameworkExtraBundle\Configuration\Route;
use Symfony\Bundle\FrameworkBundle\Controller\Controller;
use Symfony\Component\Form\Extension\Core\Type\CheckboxType;
use Symfony\Component\Form\Extension\Core\Type\EmailType;
use Symfony\Component\Form\Extension\Core\Type\SubmitType;
use Symfony\Component\Form\Extension\Core\Type\TextType;
use Symfony\Component\Form\SubmitButton;
use Symfony\Component\HttpFoundation\Request;
use AppBundle\Entity\News;

class DefaultController extends Controller
 {
     /**
      * @Route("/", name="homepage")
      */
public function indexAction(\Swift_Mailer, Request $request) //Here's the error
{
    $em = $this->getDoctrine()->getManager();

    $NewsLetters = new News();

    $formBuilder = $this->createFormBuilder($NewsLetters);
    $formBuilder
        ->add('email', EmailType::class)
        ->add('nom', TextType::class, array(
            'required' => false,
        ))
        ->add('newsWebsite', CheckboxType::class)
        ->add('newsStylo', CheckboxType::class, array(
            'required' => false,
        ))
        ->add('newsCrayon', CheckboxType::class, array(
    'required' => false,
        ))
        ->add('newsFeutre', CheckboxType::class, array(
    'required' => false,
        ))
        ->add('valider', SubmitType::class);

    $form = $formBuilder->getForm();
    $form->handleRequest($request);

    if($form->isValid() && $form->isSubmitted()) {
        $NewsLetters = $form->getData();

        if(!$NewsLetters->getNom()) {
            $NewsLetters->setNom("Anonyme");
        }

        $message = (new \Swift_Message('Confirmation d\'inscription à la newsletter'))
            ->setFrom('ez@zezezezeeze.fr')
            ->setTo('kurokncjb@gmail.com')
            ->setBody(
                $this->renderView('emails/confirmationEmalil.html.twig'), 'text/html'
            );
        $this->get('mailer')->send($message);


        $em->persist($NewsLetters);
        $em->flush();

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

感谢您的帮助!!

1 个答案:

答案 0 :(得分:0)

由于您没有使用$ mailer,

 indexAction(\Swift_Mailer, Request $request)

应替换为

 indexAction(Request $request)