denobraz / laravel-contact-form
1.0.0
2024-09-01 21:33 UTC
Requires
- php: ^8.1
README
这个小巧的 Laravel 包可以帮助您通过在配置文件中定义类型和字段,快速连接您网站的联系方式 API。
安装
要安装包,运行以下命令
composer require denobraz/laravel-contact-form
安装后,您需要发布配置文件
php artisan vendor:publish --provider="Denobraz\LaravelContactForm\ContactFormServiceProvider" --tag="config"
之后,您可以在文件 config/contact_form.php
中配置联系方式表单。
要发布迁移文件,运行以下命令
php artisan vendor:publish --provider="Denobraz\LaravelContactForm\ContactFormServiceProvider" --tag="migrations"
之后,您可以运行迁移
php artisan migrate
使用方法
注册路由
要使用联系方式表单,请将以下代码添加到您的 API 路由中
Route::post('/contact-form', Denobraz\LaravelContactForm\Http\Controllers\ContactFormController::class);
定义联系方式表单类型
默认配置文件包含带有 name
、email
、phone
、message
字段的 default
联系方式表单类型。
'types' => [ // `default` is the name of the contact form type 'default' => [ 'data' => [ // Here is rules for validation 'name' => 'string|required', 'email' => 'string|required|email', 'phone' => 'string|nullable', 'message' => 'string|nullable', ], 'messages' => [ // If you want to override the default message for some field // You can left this array empty 'name.required' => 'Name is required', ], 'attributes' => [ // If you want to override the default attribute name for some field // You can left this array empty 'name' => 'Name', ], 'callbacks' => [ // Here is the list of callbacks that will be called after the form is validated // You can left this array empty (maybe just for record form data in the database) Denobraz\LaravelContactForm\Callbacks\DummyContactFormCallback::class, ] ], // `newsletter` is the name of the contact form type 'newsletter' => [ 'data' => [ 'email' => 'string|required|email', ], ] ]
您可以为任何类型的联系方式表单附加回调(通知管理员、回复客户、将申请发送到 CRM 等...)。
任何回调都是扩展了 Denobraz\LaravelContactForm\Callbacks\ContactFormCallback
的 Job 类。
如果您想创建可排队的回调,可以扩展 Denobraz\LaravelContactForm\Callbacks\QueueableContactFormCallback
类。
此外,配置允许您:(别忘了通知用户关于敏感数据处理)
save_contact_forms
- 如果您想要将表单数据存储到数据库中save_cookies
- 如果您想要存储用户的 cookiessave_ip
- 如果您想要存储用户的 IPsave_user_agent
- 如果您想要存储用户的用户代理save_referrer
- 如果您想要存储用户的 referrersave_user_id
- 如果您想要存储用户的 ID
示例
以下是一个将电子邮件发送给管理员的回调示例
namespace App\ContactForm\Callbacks; use App\Notifications\ManagerContactFormNotification; use Denobraz\LaravelContactForm\Callbacks\ContactFormCallback; use Illuminate\Support\Facades\Notification; class SendManagerEmail extends ContactFormCallback { public function handle(): void { // You can access the contact form data using the following methods: $email = $this->contactForm->data('email'); // To use the cookies, and meta-data, you need allow storing them in the config file. $fbpCookie = $this->contactForm->cookie('fbp'); $ip = $this->contactForm->ip(); $userAgent = $this->contactForm->userAgent(); $referer = $this->contactForm->referer(); $userId = $this->contactForm->userId(); $someOtherMeta = $this->contactForm->meta('some_other_meta'); // In the notification class we pass the contact form model with data $notification = new ManagerContactFormNotification($this->contactForm); Notification::route('mail', 'admin@test.com')->notify($notification); } }
对 /api/contact-form
的请求
{ "type": "default", "data": { "name": "John Doe", "email": "example@test.com", "phone": "+1234567890", "message": "Hello, world!" } }
错误响应
{ "message": "Name is required (and 1 more error)", "errors": { "data.name": [ "Name is required" ], "data.email": [ "The Email field is required." ] } }
成功响应
{ "success": true }
您可以在 demo
目录中查看前端代码示例。