symfony getPartial不存在

时间:2013-09-11 08:59:24

标签: symfony1

我的动作类中有一个私有函数sendEmail

private function sendEmail($args)
{
    $mailer = sfContext::getInstance()->getMailer();
    $message = $this->getMailer()->compose();
    $address = $this->getFromAddress();
    $message->setFrom(array($address['email'] => $address['fullname']));
    $message->setTo('blah@blah.com');
    $message->setSubject('Subject');
    $message->setBody($this->getPartial('emailTemplate', array('args' => $args)), 'text/html');
    $this->getMailer()->send($message);
}

我从Register操作中调用该函数:

public function executeRegister(sfRequest $request)
    {
        sfConfig::set('app_sfForkedApply_from', array(
            'email' => 'blah@blah.com',
            'fullname' => 'fullname'
        ));

        //code for registering

              try {
                    // send email
                    $this->sendEmail($args);
               }
                catch (Exception $e) {
                    $this->logMessage('Email not sent: ' . $e->getMessage(), 'err');
                }

                $out = array('status' => 'success', 'args' => args);
            }            
        }

        $this->getResponse()->setContentType('application/json');
        $this->getResponse()->setContent(json_encode($out));

        return sfView::NONE;
    }

我现在想从同一个动作类中的另一个动作sendEmail调用Register2函数。

public function executeRegister2(sfRequest $request)
    {
        sfConfig::set('app_sfForkedApply_from', array(
            'email' => 'blah@blah.com',
            'fullname' => 'fullname'
        ));

        //code for registering #2

        if ($var = true) {
            try {

                // sending different email
                $this->sendVerificationMail($args);

                $out = array(
                    'status' => 'success',
                    'message' => 'Verification email sent'
                );
            }
            catch (Exception $e) {
                $this->logMessage('Verification email not sent: ' . $e->getMessage(), 'err');

                $out = array(
                    'status' => 'error',
                    'message' => 'Verification email not sent: ' . $e->getMessage()
                );
            }
        }
        else {
            //do other stuff

            $out = array(
                'status' => 'success',
                'message' => 'User activated'
            );
        }

     //added following try-catch to send new email but its not working
        try {
            // send email
            $this->sendEmail($args);

        }
        catch (Exception $e) {
            $this->logMessage('Email not sent: ' . $e->getMessage(), 'err');
        }

        $this->getResponse()->setContentType('application/json');
        $this->getResponse()->setContent(json_encode($out));

        return sfView::NONE;
    }

Register行动起作用。
Register2操作,它返回以下错误:

Email not sent: The template "_emailTemplate.json.php" does not exist or is unreadable in "".  

如果我从一个动作调用函数而不是从另一个动作调用函数,它怎么能找到模板呢? 是否总是将模板转换为json文件?这可能是问题吗?

1 个答案:

答案 0 :(得分:1)

sfView使用您的请求格式获取模板的名称。因此,如果您的操作executeRegister2是Json,它会查找.json.php文件。

你可以在sendMail方法中作弊:

private function sendEmail($args)
{
    $mailer = sfContext::getInstance()->getMailer();

    // Added
    $request = sfContext::getInstance()->getRequest();
    $format  = $request->getRequestFormat();
    $request->setRequestFormat('html');


    $message = $this->getMailer()->compose();
    $address = $this->getFromAddress();
    $message->setFrom(array($address['email'] => $address['fullname']));
    $message->setTo('blah@blah.com');
    $message->setSubject('Subject');
    $message->setBody($this->getPartial('emailTemplate', array('args' => $args)), 'text/html');
    $this->getMailer()->send($message);

    // Revert the request format
    $request->setRequestFormat($format);
}

这应该做的伎俩(就是说,在请求需要时间的时候发送电子邮件,并且弄乱你的请求格式:使它们异步的另一个原因)。