depa / middleware-formularhandler
中间件 formularhandler 是一个 PSR-15 中间件,它提供在 Zend Expressive 应用程序中处理表单数据的处理程序
Requires
- psr/container: ^1.0
- psr/http-server-middleware: ^1.0
- swiftmailer/swiftmailer: ^6.2
- true/punycode: ^2.1
- twig/twig: ^2.11
- zendframework/zend-diactoros: ^2.1
- zendframework/zend-json: ^3.1
- zendframework/zend-problem-details: ^1.0
- zendframework/zend-servicemanager: ^3.4
README
此库允许您处理表单,检查缺失字段,并且只使用您真正期望提交的字段。
安装
运行以下命令安装此库
$ composer require depa/middleware-formularhandler
信息
如果您的表单中有未在配置中定义的字段,则由于安全原因,处理程序将不会使用它们。
文档
在文档底部,我会向您展示如何快速构建配置的示例。
实现
要实现中间件,只需将路由添加到您的路由文件中,使其将请求传递到中间件
$app->route( '/formhandler[/]', [ depa\FormularHandlerMiddleware\FormularHandlerMiddleware::class, ], ['POST'], 'formHandler' );
之后,请确保在您的 config/autoload 文件夹中提供一个包含中间件检查表单所需内容的配置文件。我们建议您使用我们的配置文件,并根据您的需求进行调整
所需数据
表单处理程序需要 JSON 请求才能正确运行,并且它还会以 JSON 响应描述正在发生的事情。
适配器
目前有 2 个工作适配器
-
phpmail
定义如下'adapter' => [ 'phpmail' => [ 'reply-to' => [ 'status' => true, 'field' => 'mail', ], 'recipients' => ['example@example.com'], 'subject' => 'subject', 'sender' => 'sender@example.com', 'senderName' => 'Form', 'template' => 'Test {{message}} {{mail}}', ], ],
phpmail 通过您可能预期的 php 方法:mail() 发送邮件。
-
smtpmail
定义如下'adapter' => [ 'smtpmail' => [ //same as on phpmail but includes: 'email_transfer' => [ 'method' => 'smtp', 'config' => [ 'service' => 'smtp.googlemail.com', 'port' => '465', 'encryption' => 'ssl', 'email' => 'example@gmail.com', 'password' => 'examplepw', ], ], ], ],
smtpmail 通过 Swift_SmtpTransport 发送邮件。
您可以(如后所述)实现它们作为全局或局部适配器,全局适配器将覆盖局部适配器。
本地适配器
适配器字段必须直接位于表单定义中
'forms' => [ 'contact' => [ 'fields' => [ ... ], 'adapter' => [ ... ], ], ],
全局适配器
全局适配器定义在配置的顶部
'depaForm' => [ 'adapter' => [ 'globalTestAdapter-1' => [ 'smtpmail' => [ 'recipients' => ['example@example.com'], 'subject' => 'base subject all forms that uses this specific adaper has', 'sender' => 'example@example.com', 'senderName' => 'form', 'template' => 'nothing', 'email_transfer' => [ 'config' => [ 'service' => 'smtp.googlemail.com', 'port' => '465', 'encryption' => 'ssl', 'email' => 'example@gmail.com', 'password' => 'examplepw', ], ], ], ], ], ],
如果您已定义全局适配器并希望使用它,请将其名称(在此情况下:globalTestAdapter-1)放入表单配置的适配器字段中
'forms' => [ 'contact' => [ 'fields' => [ ... ], 'adapter' => [ 'globalTestAdapter-1' //or any other name you have used for an adapter. ], ], ],
电子邮件模板
您在配置中指定的模板可以通过 twig 动态化
'template' => 'my Name is {{name}}'
您使用的变量必须是您的表单的有效字段,并且已在您的配置中定义。
电子邮件主题
与电子邮件模板一样,电子邮件主题也支持 twig 渲染器。
回复到头
电子邮件适配器可以处理“回复到”电子邮件头,您可以在适配器配置中定义它,如下所示
'reply-to' => [ 'status' => true, 'field' => 'mail' ],
只有当以下条件满足时,reply-to 才有效
- reply-to 已定义且以下正确:
- 状态已定义且为 true;
- 字段已定义(并且存在于配置中)或未定义(但此时您的配置中必须有一个字段类型为电子邮件)
'forms' => [ 'contact' => [ 'fields' => [ 'someFieldName' => [ 'type' => 'email', ], ], 'adapter' => [ ... ], ], ],
必需属性
表单处理程序可以检查您的表单中是否存在必填字段,并在缺失时拒绝请求。如果您未定义必需或必需为 false,则处理程序可能无法获取字段的值,因为请求中缺失了该字段。如果您想将字段设置为必需,请将其添加到字段的配置中
'required' => true
示例
'depaForm' => [ 'adapter' => [ 'globalExampleAdapter-1' => [ 'smtpmail' => [ 'recipients' => ['recipient@example.com'], 'subject' => 'base subject all forms that uses this specific adaper has', 'sender' => 'example@example.com', 'senderName' => 'Kontaktformular', 'template' => 'nothing', 'email_transfer' => [ 'config' => [ 'service' => 'smtp.example.com', 'port' => '465', 'encryption' => 'ssl', 'email' => 'example@example.com', 'password' => 'yourPassword', ], ], ], ], ], 'forms' => [ 'contact' => [ 'fields' => [ 'name' => [ 'required' => true, ], 'company' => [ ], 'street' => [ ], 'city' => [ ], 'country' => [ 'required' => true, ], 'phone' => [ 'required' => true, ], 'mail' => [ 'required' => true, 'type' => 'email', ], 'message' => [ 'required' => true, ], ], 'adapter' => [ 'globalExampleAdapter-1' ], ], 'otherForm' => [ 'fields' => [ 'name' => [ 'required' => true, ], 'company' => [ ], 'street' => [ ], 'city' => [ ], 'country' => [ 'required' => false, ], 'phone' => [ 'required' => true, ], 'mail' => [ 'required' => true, 'type' => 'email', ], 'message' => [ 'required' => true, ], ], 'adapter' => [ 'phpmail' => [ 'reply-to' => [ 'status' => true, 'field' => 'mail' ], 'recipients' => ['example@example.com'], 'subject' => 'example subject', 'sender' => 'sender@example.com', 'senderName' => 'Form', 'template' => 'Test {{message}} {{mail}}', ], ], ], ], ],
鸣谢
此捆绑包由designpark开发。
许可证
MIT 许可证(MIT)。有关更多信息,请参阅许可证文件。