首页 > 解决方案 > Symfony/php:在 null 上调用成员函数 render()

问题描述

我开始了一个关于 symfony 的学校项目,我正在尝试通过 Swift_Mailer Bundle 为 symfony 发送一些邮件,但我处于死胡同

我正在创建一个邮件程序和渲染器的实例来发送邮件并渲染邮件,但即使使用我的 __construct() 方法,它们似乎也保持为空。

我真的不知道我在这搞砸了什么

你可以帮帮我吗 ?

用户控制器.php

<?php

namespace App\Controller;

use App\Entity\User;
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\Routing\Annotation\Route;
use App\Notification\ContactNotification;

class UserController extends AbstractController
{
    /**
     * @Route("/user", name="user")
     */
    public function createUser(ContactNotification $notification): Response
    {
        // you can fetch the EntityManager via $this->getDoctrine()
        // or you can add an argument to the action: createProduct(EntityManagerInterface $entityManager)
        $entityManager = $this->getDoctrine()->getManager();

        $user = new User();
        $user->setMailAddress('jhondoe@gmail.com');


        // tell Doctrine you want to (eventually) save the Product (no queries yet)
        $entityManager->persist($user);

        // actually executes the queries (i.e. the INSERT query)
        $entityManager->flush();
        $notification->notify($user);

        return new Response('Saved new user with email '.$user->getMailAddress());

    }
}

User.php 实体(非常基本,只是一个电子邮件属性,用于发送带有数据库 ID 的邮件)

<?php

namespace App\Entity;

use App\Repository\UserRepository;
use Doctrine\ORM\Mapping as ORM;

/**
 * @ORM\Entity(repositoryClass=UserRepository::class)
 */
class User
{
    /**
     * @ORM\Id
     * @ORM\GeneratedValue
     * @ORM\Column(type="integer")
     */
    private $id;

    /**
     * @ORM\Column(type="string", length=255)
     */
    private $MailAddress;

    public function getId(): ?int
    {
        return $this->id;
    }

    public function getMailAddress(): ?string
    {
        return $this->MailAddress;
    }

    public function setMailAddress(string $MailAddress): self
    {
        $this->MailAddress = $MailAddress;

        return $this;
    }
}

以及应该初始化邮件程序和渲染器的 ContactNotification.php

<?php
namespace App\Notification;

use App\Entity\User;
use Twig\Environment;

class ContactNotification {

    /**
     * @var \Swift_Mailer
     */
    private $mailer;

    /**
     * @var Environnement
     */
    private $renderer;

    
    public function __contrsuct( \Swift_Mailer $mailer, Environment $renderer){
        $this->mailer = $mailer;
        $this->renderer = $renderer; 
    }

    public function notify(User $user){
        dump($this->mailer);
        dump($this->renderer);
        $message = (new \Swift_Message('Mail test'))
        ->setFrom('noreply@test.fr')
        ->setTo('contact@soutenance.fr')
        ->setReplyTo($user->getMailAddress());
        /* ->setBody($this->renderer->render(
            // templates/emails/registration.html.twig
            'email/contact.html.twig',
            ['user' => $user]
        ),
        'text/html'
    );  */
        $this->mailer->send($message);
    }
}

标签: phpsymfonyswiftmailer

解决方案


推荐阅读