目前,easyswoole已经成为了最知名的swoole框架之一,本人也用easyswoole开发过很多个项目了,现在就来讲一讲如何用easyswoole快速实现一个网站的curd功能的接口。
安装easyswoole和相关组件
环境方面本人不多做说明,可以去官方文档查看。
新增composer.json文件{
"require": {
"easyswoole/easyswoole": "^3.2",
"easyswoole/mysqli": "^1.2",
"tioncico/curd-automatic-generation": "^1.0"
},
"autoload": {
"psr-4": {
"App\\": "Application/"
}
}
}
然后输入以下命令进行引入,安装组件库:composer up
mkdir -p Application/HttpController
php ./vendor/easyswoole/easyswoole/bin/easyswoole install
设计数据表
假设我们需要做一个简单的文章管理系统,需要用户,文章,评论,置顶,分类,这5个表:
新增文件 test.php<?php
/**
* Created by PhpStorm.
* User: tioncico
* Date: 19-7-27
* Time: 上午11:40
*/
include "./vendor/autoload.php";
//会员列表
$result = \EasySwoole\Mysqli\DDLBuilder\DDLBuilder::table('user_list', function (\EasySwoole\Mysqli\DDLBuilder\Blueprints\TableBlueprint $blueprint) {
$blueprint->colInt('id', '11')->setColumnComment('主键id')->setIsPrimaryKey()->setIsAutoIncrement();
$blueprint->colVarChar('userAccount', '32')->setColumnComment('会员账号');
$blueprint->colVarChar('userName', '32')->setColumnComment('会员昵称');
$blueprint->colVarChar('userPassword', '32')->setColumnComment('会员密码');
$blueprint->colDateTime('addTime')->setColumnComment('新增时间');
$blueprint->colTinyInt('isAdmin', 1)->setColumnComment('是否会管理员')->setDefaultValue(0);
$blueprint->setTableComment('会员列表');
$blueprint->setTableEngine(\EasySwoole\Mysqli\DDLBuilder\Enum\Engines::INNODB);
$blueprint->setTableCharset(\EasySwoole\Mysqli\DDLBuilder\Enum\Character::UTF8_GENERAL_CI);
$blueprint->indexNormal('userAccount', ['userAccount']);
});
echo $result;
//文章分类列表
$result = \EasySwoole\Mysqli\DDLBuilder\DDLBuilder::table('article_list', function (\EasySwoole\Mysqli\DDLBuilder\Blueprints\TableBlueprint $blueprint) {
$blueprint->colInt('id', '11')->setColumnComment('主键id')->setIsPrimaryKey()->setIsAutoIncrement();
$blueprint->colInt('pid', 11)->setColumnComment('上级id');
$blueprint->colVarChar('categoryName', '64')->setColumnComment('分类名称');
$blueprint->setTableComment('分类列表');
$blueprint->setTableEngine(\EasySwoole\Mysqli\DDLBuilder\Enum\Engines::INNODB);
$blueprint->setTableCharset(\EasySwoole\Mysqli\DDLBuilder\Enum\Character::UTF8_GENERAL_CI);
});
echo $result;
//文章列表
$result = \EasySwoole\Mysqli\DDLBuilder\DDLBuilder::table('article_list', function (\EasySwoole\Mysqli\DDLBuilder\Blueprints\TableBlueprint $blueprint) {
$blueprint->colInt('id', '11')->setColumnComment('