PHP code example of kicoephp / src
1. Go to this page and download the library: Download kicoephp/src library . Choose the download type require .
2. Extract the ZIP file and open the index.php.
3. Add this code to the index.php.
<?php
require_once('vendor/autoload.php');
/* Start to develop here. Best regards https://php-download.com/ */
kicoephp / src example snippets
oe\core\Link;
$link = new Link();
$link->route('/hello/{word}', function (string $word) {
return "hello ".$word;
});
$link->start();
Route::get('/art/{page}', 'Article@list');
Route::get('/art/{id}/comments', 'Comment@list');
Route::get('/art/detail/{id}', 'Article@detail');
use kicoe\core\Link;
use kicoe\core\Route;
use app\controller\ArticleController;
$link = new Link();
// 自动解析类方法注释 eg: @route get /index/{id}
Route::parseAnnotation(ArticleController::class);
// 一般 routing
$link->route('/article/tag/{tag_id}/page/{page}/', [ArticleController::class, 'listByTag']);
// 闭包
$link->route('/comment/up/{art_id}', function (Request $request, int $art_id) {
$email = $request->input('email');
...
}, 'post');
$link->start();
$link = new Link();
$link->bind('array', function (string $value):array {
return explode(',', $value);
});
$link->bind('bool', function (string $value):bool {
if ($value === 'false') {
return false;
} else if ($value === 'true') {
return true;
}
return false; // ??
});
// 访问 /1,2,3/true 将返回 ['ids' => ['1', '2', '3'], 'is_update' => true]
$link->route('/{ids}/{is_update}', function (array $ids, bool $is_update) {
return [
'ids' => $ids,
'is_update' => $is_update
];
});
$link->start();
$link = new \kicoe\core\Link();
$link->route('/tag/{tag_id}', function (\kicoe\core\Request $request) {
$request->input('name');
...
}, 'put');
$link->start();
$link = new \kicoe\core\Link();
$link->route('/tag/{tag_id}', function (\kicoe\core\Response $response, int $tag_id) {
if (!$tag = search($tag_id)) {
return $response->status(404);
}
...
return $response->json($tag);
}, 'get');
$link->start();
$link = new \kicoe\core\Link();
class CommentRequest extends \kicoe\core\Request
{
// 没有默认值的属性将作为必要参数
public int $to_id = 0;
public string $name;
public string $email;
public string $link = '';
public string $content;
/**
* @return string error 错误信息
*/
public function filter():string
{
$this->name = htmlspecialchars($this->name);
$this->email = htmlspecialchars($this->email);
$this->link = htmlspecialchars($this->link);
$this->content = htmlspecialchars($this->content);
if (!preg_match(
'/^[_a-z0-9-]+(\.[_a-z0-9-]+)*@[a-z0-9-]+(\.[a-z0-9-]+)*(\.[a-z]{2,})$/',
$this->email
)) {
return 'email 格式错误';
}
return '';
}
}
class ApiResponse extends \kicoe\core\Response
{
public int $code = 200;
public string $message = '';
/** @var Comment[] */
public array $data = [];
public function setBodyStatus(int $code, string $message):self
{
$this->code = $code;
$this->message = $message;
return $this;
}
}
// 自动注入Request和Response对象
$link->route('/{aid}', function (CommentRequest $request, ApiResponse $response, int $aid) {
if ($err = $request->filter()) {
return $response->setBodyStatus(422, 'ValidationError: '.$err);
}
Comment::insert([
'art_id' => $aid,
... // merge request data
]);
$response->data = Comment::where('art_id', $aid)->get();
// {"code":200,"message":"","data":[{...}]}
return $response;
}, 'post');
try {
$link->start();
} catch (Exception $e) {
// 根据实际注入的Response来处理异常
$response = Link::make(\kicoe\core\Response);
if ($response instanceof ApiResponse) {
$response->setBodyStatus(500, $e->getMessage())->send();
}
}
$link = new Link([
'mysql' => [
'db' => 'test',
'host' => 'mysql',
'port' => 3306,
'user' => 'root',
'passwd' => '123456',
'charset' => 'utf8mb4',
]
]);
// 可以直接执行 sql
DB::select('select id from article where id in (?) and status = ?', [1, 2, 3], 2);
DB::insert('insert into article(title, status) values (?,?), (?)', 'first', 1, ['second', 2]);
...
/**
* Class Model
* @package kicoe\core
* @method array select(...$columns)
* @method self where(string $segment, ...$params)
* @method self orWhere(string $segment, ...$params)
* @method self orderBy(...$params)
* @method self limit(...$params)
* @method self join(...$params)
* @method self leftJoin(...$params)
* @method self rightJoin(...$params)
* @method self having(...$params)
* @method self columns(...$params)
* @method self addColumns(...$params)
* @method self removeColumns(...$params)
* @method self from(string $table)
* @method array get()
* @method self first()
* @method self groupBy(string $segment)
* @method int save()
* @method int count()
* @method int delete()
* @method int update(array $data)
* @method static int insert(...$data)
* @method static static fetchById($id)
*/
class Model
DB::table('tag')->where('id in (?)', [1, 2])->selete('id', 'name');
DB::table('tag')->where('color', 'aqua')
->where('deleted_at is null')
->orderBy('id', 'desc')
->limit(0, 10)
->get();
DB::table('tag')->insert(['name' => 'php', ...], ['name' => 'golang', ...]);
DB::table('tag')->where('id', 12)->update(['name' => 'php', ...]);
DB::table('tag')->where('id', 12)->delete();
$title = '123';
$tag_id = 10;
DB::transaction(function () use ($title, $tag_id) {
$article = new Article();
$article->title = $title;
$article->save();
DB::table('article_tag')->insert([
'art_id' => $article->id,
'tag_id' => $tag_id,
]);
// throw any Exception tigger DB::rollBack()
});
namespace app\model;
use kicoe\core\Model;
class Article extends Model
{
// 默认类名小写
const TABLE = 'article';
// 默认'id'
const PRIMARY_KEY = 'id';
public int $id;
public string $title;
public int $status;
public string $image;
public string $summary;
public string $content;
public string $updated_time;
public string $created_time;
// public ?string $deleted_at;
protected array $tags;
const STATUS_DRAFT = 1;
const STATUS_PUBLISH = 2;
...
}
use app\model\Article;
// Article 对象
$art = Article::fetchById(1);
$art = new Article();
$art->title = 'new blog';
// int rowCount
$art->save();
// int
echo $art->id;
$arts = Article::where('status', Article::STATUS_PUBLISH)
->where('deleted_at is null')
->where('id in (?)', [1, 2, 3])
->orderBy('created_time', 'desc')
->limit(0, 10);
// int where 条件下的总数
$count = $arts->count();
// array Article[]
$article_list = $arts->get();
$articles = Article::get();
foreach ($articles as $article) {
$article->title = '12';
$article->save();
}
Article::update(['title' => '12']);
$article = Article::first();
$article->title = '12';
// update article set title = ? where id = ? limit ? ["12", 2, 1]
$article->save();
// 再 save 一遍不会执行任何语句
$article->save();
location / {
try_files $uri $uri/ /index.php?$query_string;
}