您可以在组件部分目录、主题的部分目录中添加表单的 HTML,或者直接将其添加到任何页面/布局。这真的无所谓。
阅读更多关于包括偏音的信息
{% partial "contact-form.htm" %}
或者
{% partial __SELF__ ~ "::contact-form.htm" %} // reference to component's partial
十月的 AJAX 框架需要使用 JavaScript API 或数据属性。您在示例中的操作方式很好,但忘记在 onSend 处理程序之前添加组件的名称
data-request="SendEmails::onSend"
where SendEmails
= 页面中给出的组件名称或别名,如果表单在组件的部分中,则使用{{ __SELF__ }}::onSend
或使用 JavaScript API,只需执行以下操作:
$.request('onSend', {
data:{email:email, message:message, name:name},
success: function (data) {
//
},
error:function(e){
//
}
});
然后在处理请求的组件中创建一个函数onSend
:
<?php namespace AuthorName\PluginName\Components;
use Cms\Classes\ComponentBase;
use Mail;
use Url;
use Input;
use Request;
use Response;
use ApplicationException;
use Validator;
use ValidationException;
class SendEmails extends ComponentBase
{
public function onSend()
{
if (Request::ajax()) {
try {
$data = post();
// Quick Validation rules for E-mail, Name & Message
if (!array_key_exists('email', $data)) {
$data['email'] = post('email');
}
if (!array_key_exists('norad', $data)) {
$data['message'] = post('message');
}
if (!array_key_exists('name', $data)) {
$data['name'] = post('name');
}
$rules = [
'email' => 'required|email|between:6,255',
'name' => 'required|between:4,255'
//..
];
$validation = Validator::make($data, $rules);
if ($validation->fails()) {
throw new ValidationException($validation);
}
// Check if E-mail Template Exists @ "author.plugin::mail.templatename"
if (View::exists("author.plugin::mail.templatename")) {
Mail::send("author.plugin::mail.templatename", $data, function ($message) {
$message->from('noreply@yourdomain.com', 'Site Name');
$message->to($data['email'], $data['name']);
$message->subject('Subject here..');
});
// Handle Erros
if (count(Mail::failures()) > 0) {
echo "Failed to send Mail "; // Handle Failure
} else {
// Mail sent
echo "Mail Sent!"; // Handle Success
}
}
} catch (Exception $ex) {
throw $ex;
}
}
}
}