Zend Framework的多阶段 - 在哪里配置/存储表单电子邮件?

时间:2011-03-31 14:20:35

标签: zend-framework

在我们的开发服务器上,当我们使用表单并对其进行测试时,通常我们会使用自己的电子邮件,以便在测试时,我们可以收到这些结果并查看它是否全部正常工作。

当我们更改为制作时,我们必须将这些电子邮件更改为“生产”电子邮件。

通常,我们倾向于忘记这一点,并且我们不会接收到我们不应该接收的数据,同时,我们的生产受益者没有得到他们应该得到的东西。

我们如何处理这个问题,根据环境变量定义,我们可以在表单提交或其他电子邮件中使用一封电子邮件?

更新 - 重要说明:

1)

我不关心状态消息或我们使用的电子邮件,同时压力测试奇怪的东西,没有错误通知,没有类似的。

这是一个非常简单的概念:   - 我们有一些订阅表格和联系表格。在prod ENV上,将对应两三个不同的电子邮件。仅此而已。

2)

让我们假设我们正在扩展Zend_Form以创建表单,并且我们在这些行中(或多或少)有一些东西:

$this->request->setMethod('POST')
->setPost(array(
'name'
=> 'Some Name',
'email'
=> 'contact@email.com',
'message'
=> "This is my test message."
));
$this->dispatch('/about/contact');

更新2:

我的application.ini文件可以或应该具有以下内容:

[production]
contact.email.address = "trueemail@email.com"
contact.email.name = "John Stuart"

joinus.email.address = "anotheremail@email.com"
joinus.email.name = "Patricia Bill"


[development : production]
contact.email.address = "my@email.com"
contact.email.name = "Devname"

joinus.email.address = "my@email.com"
joinus.email.name = "Devname"

或者我们应该以另一种方式做到这一点吗?

如果我们有10个不同的电子邮件,这仍然是一项艰苦的工作和重复,因为,如果我们有10个表格,或开发环境电子邮件将重复10次! :■

我在这里的另一个疑问是,一旦在application.ini上定义了这个:

如何拨打这些电子邮件地址和姓名?

这样做的可接受方式是什么?

2 个答案:

答案 0 :(得分:0)

如果不知道应用程序的结构,表单的创建方式以及收件人详细信息的来源,这很难回答。但是,您不能简单地在设置电子邮件地址的位置检查APPLICATION_ENV吗?

if (APPLICATION_ENV == 'development') {
    $email = 'yourtestemail@example.com'; // your test email address
} else {
    $email = 'normalemail@example.com'; // whatever the email should be
}

编辑:由于您暗示通过扩展Zend_Form来创建表单,如何为包含上述逻辑的联系表单添加基类?像这样:

class Yourapp_Form extends Zend_Form
{
    protected $_recipientEmail;

    public function setRecipientEmail($email)
    {
        $this->_recipientEmail = $email;
    }

    public function getRecipientEmail()
    {
        if (APPLICATION_ENV == 'develpoment') {
            return 'yourtestemail@example.com';
        } else {
            return $this->_recipientEmail;
        }
    }
}

然后你会以类似于当前使用Zend_Form的方式使用它,所以类似于:

class Yourapp_Contact_Form extends Yourapp_Form
{
    public function init()
    {
        $this->addElement([...]);
        $this->setRecipientEmail('foo@example.com'); // <-- production email address
    }
}

答案 1 :(得分:0)

我真的不确定你想要什么,但是,如果您正在寻找的是根据您的环境处理某些逻辑的方法,这是一个解决方案。

编写一个服务,你可以在任何地方加载(prod,dev等)。此服务将负责检查是否应根据您的APPLICATION_ENV通知您。

对硬编码if条件使用服务的主要优点是允许您不关心方法的可用性。

该组件的行为方式与Zend_Log类似,根据您的使用情况,此组件可能更适合您的需求。

使用Zend_Log,您可以根据APPLICATION_ENV中的config.ini设置过滤优先级。

[development]
resources.log.stream.filterName = "Priority"
resources.log.stream.filterParams.priority = Zend_Log::DEBUG
[production]
resources.log.stream.filterName = "Priority"
resources.log.stream.filterParams.priority = Zend_Log::DEBUG

Zend_Log提供了几个所谓的写作,包括文件,电子邮件,萤火虫等。

最后,尝试最小化(如果有)使用“APPLICATION_ENV特定功能,组件等”如果您需要有用的调试信息,请使用提供的工具(如Zend_Log,FirePHP,配置工具和模拟对象)。


根据您的更新进行编辑:

即使您需要“重复”x10,您的解决方案也绝对可以接受。但是,我会采用略有不同的方式。

类似的东西:

[production]
app.contacts.service1.name    = "Paul Dupont"
app.contacts.service1.email   = "paul.dupont@example.com"
app.contacts.service1.company = "Another Best Company"
app.contacts.service1.name  = "Kristina Dupuis"
app.contacts.service1.email = "k.dupuis@example.com"
[development]
app.contacts.service1.name    = "Me"
app.contacts.service1.email   = "me@example.com"
app.contacts.service1.company = "THE best company"
app.contacts.service1.name  = "Myself"
app.contacts.service1.email = "myself@example.com"

是的,如果你有很多服务,它会有点沉重,但它比任何硬编码解决方案都更灵活。

要使用它,我会选择:

  1. action helper,动作助手只能在Controller中使用,方便快捷
  2. 域模型对象通过Service,flexible和config.ini更新免费(如果更多服务为什么不存储在数据库中)
  3. 两者都有类似的骨架

    interface Service // Service means here the competent service of your company
    {
    
    public function getEmail() {}
    
    public function getContactName() {}
    
    public function getCompanyName() {}
    
    }
    
    // usage, somewhere in your code :
    $this->request->setMethod('POST')
    ->setPost(array(
    'name'    => $serviceService->getService('contactus')->getContactName(),
    'email'   => $serviceService->getService('contactus')->getEmail(),
    'message' => "This is my test message."
    ));
    $this->dispatch('/about/contact');