64robots / webforms
64 Robots webforms 的后端。
Requires
- php: ^7.4
- illuminate/broadcasting: ^7.0|^8.0
- illuminate/database: ^7.0|^8.0
- illuminate/http: ^7.0|^8.0
- illuminate/queue: ^7.0|^8.0
- illuminate/support: ^7.0|^8.0
- illuminate/validation: ^7.0|^8.0
Requires (Dev)
- friendsofphp/php-cs-fixer: ^2.16
- orchestra/testbench: ^5.0
- phpunit/phpunit: ^9.0
- psalm/plugin-laravel: ^1.2
- vimeo/psalm: ^3.11
This package is auto-updated.
Last update: 2024-08-29 05:50:29 UTC
README
快速创建自定义表单的包。此包为您提供了一个轻松启动单页面应用程序(SPA)表单后端的简便方法。您可以创建表单、表单步骤和问题。您的用户可以对这些表单进行回复。由 64 Robots 制作。
安装
您可以通过 composer 安装此包
composer require 64robots/webforms
您可以使用以下命令发布和运行迁移
php artisan vendor:publish --provider="R64\Webforms\WebformsServiceProvider" --tag="migrations" php artisan migrate
您可以使用以下命令发布配置文件
php artisan vendor:publish --provider="R64\Webforms\WebformsServiceProvider" --tag="config"
这是已发布 webforms.php 配置文件的内容
use R64\Webforms\QuestionTypes\EmailType; use R64\Webforms\QuestionTypes\PhoneType; return [ 'date_format' => 'Y-m-d', 'year_month_format' => 'Y-m', 'fields_to_be_confirmed' => [ EmailType::TYPE, PhoneType::TYPE, ], 'user_model' => 'App\User', ];
用法
1 - 添加路由
目前,该包不支持匿名用户。请将此添加到您的路由文件中的 auth 中间件下
Route::webforms('webforms');
如果您想在路由文件中创建表单、表单步骤和问题,请在相应的中间件下添加
Route::webformsAdmin('webforms-admin');
2 - 在您的用户实体中添加 HasWebForms 特性。
3 - 为表单、表单步骤和问题创建种子。
示例
让我们从您的应用程序中开始一个新的表单,以收集有关咖啡的信息
我们将在 routes/api.php 中添加
Route::webforms('webforms');
我们还将添加到 routes/api_admin.php
Route::webformsAdmin('webforms-admin');
下一步是添加一个新的表单。只需创建一个新的种子器。
php artisan make:seeder CoffeeSeeder
现在在种子器文件 database/seeders/CoffeeSeeder 中,我们将包括创建表单、表单步骤和问题的操作。
让我们从创建表单开始
use R64\Webforms\Models\Form; $coffeeForm = Form::build('Coffee Form') ->save();
一旦我们有了 form,我们至少需要添加一个表单步骤
use R64\Webforms\Models\FormStep; $coffeeStep = FormStep::build($coffeeForm, 'Coffee') ->save();
然后我们可以添加到该步骤的 questions
use R64\Webforms\Models\Question; use R64\Webforms\QuestionTypes\OptionsType; $whatKindOfCoffeeDoYouLikeQuestion = Question::build($coffeeStep, 'What kind of coffee do you like?') ->type(OptionsType::TYPE) ->options([ 'black' => 'Black', 'latte' => 'Latte', 'capuccino' => 'Cappucino', 'americano' => 'Americano', 'red-eye' => 'Red Eye', 'flat-white' => 'Flat White', ]) ->save(); $whatTypeOfBeansDoYouLikeQuestion = Question::build($coffeeStep, 'What type of coffee beans do you like the most?') ->type(OptionsType::TYPE) ->options([ 'arabica' => 'Arabica', 'robusta' => 'Robusta', ]) ->save();
现在添加一个新的步骤来收集一些个人信息。我们将加密数据库中的这些信息
use R64\Webforms\Models\FormStep; $personalInfoStep = FormStep::build($coffeeForm, 'Personal info') ->isPersonalData(1) ->save();
然后添加问题
use R64\Webforms\Models\Question; use R64\Webforms\QuestionTypes\IntegerType; $firstNameQuestion = Question::build($personalInfoStep, 'First Name') ->save(); $lastNameQuestion = Question::build($personalInfoStep, 'Last Name') ->save(); $ageQuestion = Question::build($personalInfoStep, 'Birth Year') ->type(IntegerType::TYPE) ->save();
一旦我们有了这些,我们就可以将问题步骤添加到用户
User::all()->each->addFormSteps([$coffeeStep, $personalInfoStep]);
如果我们想将所有表单步骤添加到用户,我们也可以使用
User::all()->each->addFormSteps();
当用户请求表单时,他们将只获得他们在其上具有步骤的表单。我们需要进行认证请求到
/webforms/forms
我们将得到类似的东西
{
"data": [
{
"id": 1,
"sort": 1,
"slug": "coffee-form",
"menu_title": null,
"title": "Coffee Form",
"description": null,
"completed": false
}
]
}
假设表单是 ID 为 1 的那个。然后我们可以再次请求
/webforms/form-steps?form=1
我们将获得所有表单步骤的信息
{
"data": [
{
"id": 1,
"form": {
"id": 1,
"sort": 1,
"slug": "coffee-form",
"menu_title": "",
"title": "Coffee Form",
"description": "",
"completed": false
},
"sort": 1,
"slug": "coffee",
"menu_title": null,
"title": "Coffee",
"description": "",
"completed": false
},
{
"id": 2,
"form": {
"id": 1,
"sort": 1,
"slug": "coffee-form",
"menu_title": null,
"title": "Coffee Form",
"description": null,
"completed": false
},
"sort": 2,
"slug": "personal-info",
"menu_title": null,
"title": "Personal info",
"description": null,
"completed": false
}
]
}
对于每个表单步骤,我们需要使用以下命令请求问题
/webforms/questions?form_step=1
{
"data": [
{
"id": 1,
"form_step": {
"id": 1,
"sort": 1,
"slug": "coffee",
"menu_title": null,
"title": "Coffee",
"description": "",
"completed": false
},
"sort": 1,
"depends_on": null,
"shown_when": null,
"required": false,
"slug": "what-kind-of-coffee-do-you-like",
"group_by": null,
"group_by_description": null,
"label_position": "left",
"help_title": null,
"help_body": null,
"type": "options",
"post_input_text": null,
"title": "What kind of coffee do you like?",
"description": null,
"error_message": null,
"default_value": null,
"min": null,
"max": null,
"options": [
{
"label": "Black",
"value": "black"
},
{
"label": "Latte",
"value": "latte"
},
{
"label": "Cappucino",
"value": "capuccino"
},
{
"label": "Americano",
"value": "americano"
},
{
"label": "Red Eye",
"value": "red-eye"
},
{
"label": "Flat White",
"value": "flat-white"
}
],
"answer": {}
},
{
"id": 2,
"form_step": {
"id": 1,
"sort": 1,
"slug": "coffee",
"menu_title": null,
"title": "Coffee",
"description": "",
"completed": false
},
"sort": 2,
"depends_on": null,
"shown_when": null,
"required": false,
"slug": "what-type-of-coffee-beans-do-you-like-the-most",
"group_by": null,
"group_by_description": null,
"label_position": "left",
"help_title": null,
"help_body": null,
"type": "options",
"post_input_text": null,
"title": "What type of coffee beans do you like the most?",
"description": null,
"error_message": null,
"default_value": null,
"min": null,
"max": null,
"options": [
{
"label": "Arabica",
"value": "arabica"
},
{
"label": "Robusta",
"value": "robusta"
}
],
"answer": {}
}
]
}
我们需要对个人信息步骤执行相同的操作
/webforms/questions?form_step=2
{
"data": [
{
"id": 3,
"form_step": {
"id": 2,
"sort": 2,
"slug": "personal-info",
"menu_title": null,
"title": "Personal info",
"description": "",
"completed": false
},
"sort": 3,
"depends_on": null,
"shown_when": null,
"required": false,
"slug": "first-name",
"group_by": null,
"group_by_description": null,
"label_position": "left",
"help_title": null,
"help_body": null,
"type": "text",
"post_input_text": null,
"title": "First Name",
"description": null,
"error_message": null,
"default_value": null,
"min": null,
"max": null,
"options": null,
"answer": {}
},
{
"id": 4,
"form_step": {
"id": 2,
"sort": 2,
"slug": "personal-info",
"menu_title": null,
"title": "Personal info",
"description": "",
"completed": false
},
"sort": 4,
"depends_on": null,
"shown_when": null,
"required": false,
"slug": "last-name",
"group_by": null,
"group_by_description": null,
"label_position": "left",
"help_title": null,
"help_body": null,
"type": "text",
"post_input_text": null,
"title": "Last Name",
"description": null,
"error_message": null,
"default_value": null,
"min": null,
"max": null,
"options": null,
"answer": {}
},
{
"id": 5,
"form_step": {
"id": 1,
"sort": 1,
"slug": "coffee",
"menu_title": null,
"title": "Coffee",
"description": "",
"completed": false
},
"sort": 4,
"depends_on": null,
"shown_when": null,
"required": false,
"slug": "birth-year",
"group_by": null,
"group_by_description": null,
"label_position": "left",
"help_title": null,
"help_body": null,
"type": "integer",
"post_input_text": null,
"title": "Birth Year",
"description": null,
"error_message": null,
"default_value": null,
"min": null,
"max": null,
"options": null,
"answer": {}
}
]
}
当用户需要回答问题,我们需要向以下位置发送 POST 请求
/webforms/answers
带有以下有效载荷
{
"question_id": 2,
"text": "arabica"
}
我们将收到带有答案的问题
{
"data": {
"id": 2,
"form_step": {
"id": 1,
"sort": 1,
"slug": "coffee",
"menu_title": null,
"title": "Coffee",
"description": "",
"completed": false
},
"sort": 2,
"depends_on": null,
"shown_when": null,
"required": false,
"slug": "what-type-of-coffee-beans-do-you-like-the-most",
"group_by": null,
"group_by_description": null,
"label_position": "left",
"help_title": null,
"help_body": null,
"type": "options",
"post_input_text": null,
"title": "What type of coffee beans do you like the most?",
"description": null,
"error_message": null,
"default_value": null,
"min": null,
"max": null,
"options": [
{
"label": "Arabica",
"value": "arabica"
},
{
"label": "Robusta",
"value": "robusta"
}
],
"answer": {
"id": 123,
"user_id": 10,
"question_id": 2,
"text": "arabica",
"confirmed": true
}
}
}
测试
将 phpunit.xml.dist 复制到 phpunit.xml
cp phpunit.xml.dist phpunit.xml
根据您的喜好调整或更改代码中的以下值
<php> <env name="DB_CONNECTION" value="mysql"/> <env name="DB_USERNAME" value="root"/> <env name="DB_PASSWORD" value=""/> <env name="DB_DATABASE" value="r64_webforms"/> <env name="DB_HOST" value="127.0.0.1"/> <env name="DB_PORT" value="3306"/> </php>
创建数据库,在本例中为 r64_webforms。
执行
composer test
变更日志
请参阅 CHANGELOG 以获取有关最近更改的更多信息。
贡献
请参阅 CONTRIBUTING 以获取详细信息。
安全性
如果您发现任何与安全相关的问题,请通过电子邮件 mmanzano@gmail.com 而不是使用问题跟踪器来报告。
鸣谢
许可证
麻省理工学院许可证(MIT)。请参阅许可证文件以获取更多信息。
致谢
感谢Spatie提供的Laravel包骨架。