调用成员函数has()on null(render method)Symfony

时间:2017-05-21 11:36:19

标签: symfony

我有2个捆绑包:

1)带有RememberController类的UserBundle

namespace UserBundle\Controller;
use Symfony\Bundle\FrameworkBundle\Controller\Controller;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\RedirectResponse;
use MailEventBundle\Controller\MailEventController;

class RememberController extends Controller
{
    public function rememberAction(Request $request)
        {
        if($request->get('email') != null){

            $params = 
            [
                'email' => $request->get('email'),
                'subject' => 'Remind password', 
                'template' => 'template/remember.html',
                'values' => array(
                    'password' => md5($request->get('email') . date("l dS of F Y h:i:s A"))
                    )
            ];

            $MailEventController = new MailEventController();

            $MailEventController->mailGenerator($params);

            return new Response(
                'Almost ok',
                Response::HTTP_OK
            );
        }
    }


}

如果我在这堂课中调用渲染方法,一切都很好!但是,如果我在MailEventController中调用render方法我有错误调用成员函数has()on null,我也调用$ this->容器并从MailEventController返回null

2)MailEventController

namespace MailEventBundle\Controller;
use Symfony\Bundle\FrameworkBundle\Controller\Controller;

class MailEventController extends Controller
{
    private function sendAction($message)
    {
        try {
            $this->get('mailer')->send($message);
            return true;
        } catch (\Swift_TransportException $e) {
            return $e;
        }
    }

    public function mailGenerator($params)
    {
       $message = \Swift_Message::newInstance()
            ->setSubject($params['subject'])
            ->setFrom('email@email.com')
            ->setTo($params['email'])
            ->setBody(
                $this->renderView( // error here
                    $params['template'],
                    $params['values']
                ),
                'text/html'
            );


       return $this->sendAction($message);

    }

}

如何解决这个问题?

0 个答案:

没有答案