报名系统

编写前端代码(简易版,前端不做对输入正误的检测)

在views目录下创建form.blade.php文件

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
<!Doctype html>
<html>
<head>
<meta charset="utf-8">
<title>报名页面</title>

</head>
<body>
<h3 align="center">报名页面</h3>
<form action={{ url('sign/form') }} method="post" >
<div class="form-group" align="center">
<label>学号</label>
<input type="text" name="stuId" class="form-control" placeholder="学号" >
</div>
<div class="form-group" align="center">
<label>姓名</label>
<input type="text" name="name" class="form-control" placeholder="名字" >
</div>
<div class="form-group" align="center">
<label>年龄</label>
<input type="text" name="age" class="form-control" placeholder="年龄" >
</div>
<div class="form-group" align="center">
<label>邮箱</label>
<input type="text" name="email" class="form-control" placeholder="邮箱" >
</div>
<div class="form-group" align="center">
<label>学院</label>
<input type="text" name="college" class="form-control" placeholder="学院" >
</div>
<div class="form-group" align="center">
<label>专业</label>
<input type="text" name="major" class="form-control" placeholder="专业" >
</div>
<div align="center">
<button type="submit" class="btn btn-primary" >提交</button>
</div>
</form>
</body>
</html>

编写控制器、路由,并测试index页面是否打通
1
2
3
4
5
6
7
8
9
signController
public function index()
{
return view('form.sign');
}

route
Route::get('sign/index', 'signController@index');
Route::post('sign/form', 'signController@form');

PS:测试前需要关闭App/Http/MiddleWare/VerifyCsrfToken.php的csrf验证

创建模型、迁移文件

在控制台下输入
php artisan make:model Sign
php artisan make:migration create_signs_table
PS:Model类默认的数据表为派生的Model类名+s,所以创建的数据表名为signs

编写迁移文件,修改.env文件,通过迁移文件在数据库中快速创建数据表
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
迁移文件
Schema::create('signs', function (Blueprint $table) {
$table->bigIncrements('id');
$table->integer('stuId');
$table->string('name');
$table->integer('age');
$table->string('email');
$table->string('college');
$table->string('major');
$table->timestamps();
});

.env文件
DB_DATABASE= database
DB_USERNAME= username
DB_PASSWORD= password

创建数据表
artisan migrate
PS:如果提示字符过长,则在App/Providers/AppserviceProvider.php的boot方法定义字符长度
Schema::defaultStringLength(225);
编写Api
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
报名APi:处理对输入的正确性检测以及向数据库写入
class ApiSign
{
/**
* 判断输入是否正确
* @param $request
* @return bool
*/
public function Judge($request)
{
$arr = [
'stuId', 'name', 'age', 'email', 'college', 'major'
];
foreach ($arr as $key) {
if($request[$key] == null) {
throw new ApiException(ApiErrorCode::ERROR_INFO_NOT_COMPLETE);
}
}
$stuId = $request['stuId'];
if($stuId < 20180000 || $stuId >= 20190000 || !is_numeric($stuId)) {
throw new ApiException(ApiErrorCode::ERROR_STUID);
}
$age = $request['age'];
if(!is_numeric($age) || $age < 18 || $age > 100) {
throw new ApiException(ApiErrorCode::ERROR_AGE_NOT_EXIST);
}
// 邮箱地址验证
if( !preg_match('/[a-zA-Z0-9]+@[a-zA-Z0-9]+.(com|cn)/', $request['email'])) {
throw new ApiException(ApiErrorCode::ERROR_EMAIL);
}
return true;
}

/**
* 向数据库中写入数据
* @param $request
*/
public function write($request)
{
$stuId = intval($request['stuId']);
$ret = DB::table('signs')->where('stuId', '=', $stuId)->first();
$age = intval($request['age']);
$time = time('Y-m-d H:i:s');
if($ret == null) {
$sign = new Sign();
$sign->stuId = $stuId;
$sign->name = $request['name'];
$sign->age = $age;
$sign->email = $request['email'];
$sign->college = $request['college'];
$sign->major = $request['major'];
$sign->save();
throw new ApiException(ApiErrorCode::SUCCESS);
}else {
throw new ApiException(ApiErrorCode::ERROR_INFO_EXIST);
}
}
}

异常Api:返回一些异常信息
class ApiException extends \RuntimeException
{
public function __construct(array $ApiErrorConst,Throwable $previous = null)
{
$code = $ApiErrorConst[0];
$message = $ApiErrorConst[1];
parent::__construct($message, $code, $previous);
}
}

class ApiErrorCode
{
/**
* 错误码定义
*/
const SUCCESS = [0, 'success'];
const ERROR_INFO_NOT_COMPLETE = [1, '填写信息不全'];
const ERROR_STUID = [2, '学号不存在'];
const ERROR_EMAIL = [3, '邮箱错误'];
const ERROR_INFO_EXIST = [4, '报名信息已存在'];
const ERROR_AGE_NOT_EXIST = [5, '年龄不正确'];
}
ORM模型操作数据库的相关配置
1
2
3
4
5
6
7
8
9
10
11
12
13
class Sign extends Model
{
/**
* 定义操作的数据表
* @var string
*/
protected $table = 'signs';
/**
* 设置时间戳格式
* @var string
*/
protected $dateFormat = 'Y-m-d H:i:s';
}
Test!
-------------本文结束  感谢您的阅读-------------