denobraz/laravel-contact-form
Composer 安装命令:
composer require denobraz/laravel-contact-form
包简介
README 文档
README
This small laravel package helps you quickly connect the contact form api for your site by simply defining the types and fields in the configuration file.
Installation
To install the package, run the command:
composer require denobraz/laravel-contact-form
After installation, you need to publish the configuration file:
php artisan vendor:publish --provider="Denobraz\LaravelContactForm\ContactFormServiceProvider" --tag="config"
After that, you can configure the contact form in the file config/contact_form.php.
To publish the migration file, run the command:
php artisan vendor:publish --provider="Denobraz\LaravelContactForm\ContactFormServiceProvider" --tag="migrations"
After that, you can run the migration:
php artisan migrate
Usage
Registering the routes
To use contact form add the following code to your api routes:
Route::post('/contact-form', Denobraz\LaravelContactForm\Http\Controllers\ContactFormController::class);
Define contact form types
Default configuration file include default contact form type with name, email, phone, message fields.
'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 leave this array empty 'name.required' => 'Name is required', ], 'attributes' => [ // If you want to override the default attribute name for some field // You can leave this array empty 'name' => 'Name', ], 'callbacks' => [ // Here is the list of callbacks that will be called after the form is validated // You can leave 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', ], ] ]
You can attach callbacks to any type of contact form (notification to the administrator, response to the client, sending an application to the CRM, etc...).
Any callback is Job-like class that extends Denobraz\LaravelContactForm\Callbacks\ContactFormCallback.
If you want to create queueable callback, you can extend Denobraz\LaravelContactForm\Callbacks\QueueableContactFormCallback class.
Also, configuration allows you: (Don't forget to notify the users about sensitive data processing)
save_contact_forms- if you want to store the form data in the databasesave_cookies- if you want to store the user's cookiessave_ip- if you want to store the user's ipsave_user_agent- if you want to store the user's user-agentsave_referrer- if you want to store the user's referrersave_user_id- if you want to store the user's id
Examples
Here is example of callback that sends email to the administrator:
namespace App\ContactForm\Callbacks; use App\Notifications\ManagerContactFormNotification; use Denobraz\LaravelContactForm\Callbacks\ContactFormCallback; use Illuminate\Support\Facades\Notification; class SendManagerEmail extends ContactFormCallback { // You must implement the `handle` method // where you can process the contact form data 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); } }
Request to /api/contact-form:
{
"type": "default",
"data": {
"name": "John Doe",
"email": "example@test.com",
"phone": "+1234567890",
"message": "Hello, world!"
}
}
Error response:
{
"message": "Name is required (and 1 more error)",
"errors": {
"data.name": [
"Name is required"
],
"data.email": [
"The Email field is required."
]
}
}
Success response:
{
"success": true
}
You can see frontend code example in the demo directory.
denobraz/laravel-contact-form 适用场景与选型建议
denobraz/laravel-contact-form 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 286 次下载、GitHub Stars 达 5, 最近一次更新时间为 2024 年 09 月 01 日, 在 PHP 生态内属于活跃度较高的组件。
我们在过去多个企业项目中使用过 denobraz/laravel-contact-form 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。
基于 denobraz/laravel-contact-form 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。
线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。
承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。
统计信息
- 总下载量: 286
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 5
- 点击次数: 6
- 依赖项目数: 0
- 推荐数: 0
其他信息
- 授权协议: MIT
- 更新时间: 2024-09-01