当前位置:网站首页>[PHP] self developed framework qphp, used by qphp framework
[PHP] self developed framework qphp, used by qphp framework
2022-06-30 23:44:00 【weixin_ forty-three million two hundred and twenty-four thousan】
QPHP
1.qphp It's a lightweight phpmvc frame
2. Support mysql,oracle,memcache,redis
3.jwt Generate token, And verification
4. Add verifier filter
composer require inhere/php-validate:dev-master
5. project application\admin,admin Is the instance code
5. Complete addition, deletion and modification (CURD) function
6. New business reference admin and index modular
7. Request address http://www.qphp.com/admin/user/index?id=10
github: GitHub - 1211884772/QPHP: qphp It's a lightweight phpmvc frame
packagist: qphp/qphp - Packagist
8. Add simple routing function
In the file route Catalog index.php,admin.php Repeated will overwrite
$route = Route::instance();
$route->get('index/age','index/index/age');
$route->get('index/name','index/index/name');
$route->post('index/name','index/index/addName');
$route->put('index/name','index/index/putName');
$route->delete('index/name','index/index/delName');
perhaps
Route::get('index/age','index/index/age');
Route::get('index/name','index/index/name');
Route::post('index/name','index/index/addName');
Route::put('index/name','index/index/putName');
Route::delete('index/name','index/index/delName');9. Global configuration function , Module configuration function
9.1. Module configuration will automatically overwrite the data of global configuration parameters
9.2. Configurations unrelated to business will only take effect in the global configuration file, such as :
'RPC_RUN'=>false,// Open or not rpc
'ROUTE_PATH'=>true,// Whether to turn on the routing mode
'APP_DEBUG'=> true,//debug// Whether the opening error is displayed on the page 10. Generate distributed id
install :
https://packagist.org/packages/dekuan/dedid
composer require dekuan/dedid11. Add sub database function , Multi library switching operation , Connections have simple connection pool management
<?php
$config['app'] = array(
// Global database configuration
'mysql_0' => array(
'host' => '127.0.0.1',
'dbname' => 'qphp',
'mysql_user' => 'qphp',
'mysql_pwd' => '123456',
'port' => 3306
),
'mysql_1' => array(
'host' => '127.0.0.1',
'dbname' => 'qphp_01',
'mysql_user' => 'root',
'mysql_pwd' => '123456',
'port' => 3306
),
'mysql_2' => array(
'host' => '127.0.0.1',
'dbname' => 'qphp_02',
'mysql_user' => 'root',
'mysql_pwd' => '123456',
'port' => 3306
),
);php Model table query , And sort
<?php
public function getUser(){
// select id,title from collect where id>=(select id from collect order by id limit 90000,1) limit 10;
$data = [];
// The second page id<35 // Paging is done by the front end , Return multiple pieces of data at one time
$sql1="select * from ((select * from q_user_01 )UNION ALL (select * from q_user_02)) as u
where u.id<35 ORDER BY u.create_time desc limit 10";
$this->getLastSql();
$data_1 = $this->Db('mysql_1')->executeSql("getRows",$sql1);
$sql2="select * from ((select * from q_user_01 )UNION ALL (select * from q_user_02)) as u
where u.id<35 ORDER BY u.create_time desc limit 10";
$data_2=$this->Db('mysql_2')->executeSql("getRows",$sql2);
$sql3="select * from ((select * from q_user_01 )UNION ALL (select * from q_user_02)) as u
where u.id<35 ORDER BY u.create_time desc limit 10";
$data_3=$this->Db('mysql_3')->executeSql("getRows",$sql3);
$data = array_merge($data_1,$data_2,$data_3);
$sort_c = array_column($data,'create_time');
$sort_u = array_column($data,'updat_time');
array_multisort($sort_c, SORT_DESC,SORT_NUMERIC,$sort_u,SORT_DESC,SORT_NUMERIC,$data);
return $data;
}10. Generate distributed id
ALGORITHM
Bit structure
It's a 64 bits bigint.
0 xxxxxxxx xxxxxxxx xxxxxxxx xxxxxxxx xxxxxxxx x xxxxx xxxxx xxxx xxxxxxxx
Details
| Position | Length | Usage | Remark |
|---|---|---|---|
| 0 | 1 | Reserved | Always be 0 |
| 1~41 | 41 | Escaped Time (in millisecond) | 0~69 years |
| 42~46 | 5 | Number of data center | 0~31 |
| 47~51 | 5 | Number of data node in the data center | 0~31 |
| 52~63 | 12 | Random / Hash | 0~4095 |
Bit marks
Center
0 00000000 00000000 00000000 00000000 00000000 0 11111 00000 0000 00000000
00000000 00000000 00000000 00000000 00000000 00111110 00000000 00000000
00 00 00 00 00 3E 00 00
Node
0 00000000 00000000 00000000 00000000 00000000 0 00000 11111 0000 00000000
00000000 00000000 00000000 00000000 00000000 00000001 11110000 00000000
00 00 00 00 00 01 F0 00
Escaped Time
0 11111111 11111111 11111111 11111111 11111111 1 00000 00000 0000 00000000
01111111 11111111 11111111 11111111 11111111 11000000 00000000 00000000
7F FF FF FF FF C0 00 00
Random or Hash value
0 00000000 00000000 00000000 00000000 00000000 0 00000 00000 1111 11111111
00000000 00000000 00000000 00000000 00000000 00000000 00001111 11111111
00 00 00 00 00 00 0F FF
HOW TO USE
Create an new id normally
$cDId = CDId::getInstance();
$nCenter = 0;
$nNode = 1;
$arrD = [];
$nNewId = $cDId->createId( $nCenter, $nNode, null, $arrD );
echo "new id = " . $nNewId . "\r\n";
print_r( $arrD );
output
new id = 114654484990270790
Array
(
[center] => 0
[node] => 1
[time] => 27335759399
[rand] => 3398
)
Create an new id with crc32 hash value by a specified string
$cDId = CDId::getInstance();
$nCenter = 0;
$nNode = 15;
$sSrc = "dekuan";
$arrD = [];
$nNewId = $cDId->createId( $nCenter, $nNode, $sSrc, $arrD );
echo "new id = " . $nNewId . "\r\n";
print_r( $arrD );
output
new id = 114654631304370386
Array
(
[center] => 0
[node] => 1
[time] => 27335794283
[rand] => 2258
)
Parse an id for getting the details
$cDId = CDId::getInstance();
$arrId = $cDId->parseId( 114654631304370386 );
print_r( $arrId );
output
Array
(
[center] => 0
[node] => 1
[time] => 27335794283
[rand] => 2258
)
1. The following is a case of global configuration , And modules admin,index Configuration of
// The global configuration file is loaded in config\ Catalog
config.php
/**
* General profile
*/
$config['app'] = array(
'RPC_RUN'=>false,// Open or not rpc
'ROUTE_PATH'=>true,// Whether to turn on the routing mode
'APP_DEBUG'=> true,//debug// Whether the opening error is displayed on the page
// Global database configuration
'mem' => array(
'host' => '127.0.0.1',
'port' => '11211'
),
'redis' => array(
'host' => '127.0.0.1',
'port' => '6379'
),
);
// The local configuration file is in application\admin\Config\ Catalog
config.php
<?php
$config['app'] = array(
// Global database configuration
'mysql_0' => array(
'host' => '127.0.0.1',
'dbname' => 'qphp',
'mysql_user' => 'qphp',
'mysql_pwd' => '123456',
'port' => 3306
),
'mysql_1' => array(
'host' => '127.0.0.1',
'dbname' => 'qphp_01',
'mysql_user' => 'root',
'mysql_pwd' => '123456',
'port' => 3306
),
'mysql_2' => array(
'host' => '127.0.0.1',
'dbname' => 'qphp_02',
'mysql_user' => 'root',
'mysql_pwd' => '123456',
'port' => 3306
),
);
// The local configuration file is in application\index\Config\ Catalog
config.php
<?php
$config['app'] = array(
// Global database configuration
'oracle_0' => array(
'host' => '192.168.123.101',
'dbname' => 'QPHP',
'oracle_user' => 'QPHP',
'oracle_pwd' => '123456',
'port' => 1521
),
// Global database configuration
'oracle_1' => array(
'host' => '192.168.123.101',
'dbname' => 'QPHP_01',
'oracle_user' => 'QPHP_01',
'oracle_pwd' => '123456',
'port' => 1521
),
);1. newly added Model Of mysql Chain query
// Inquire about
public function getUsers(){
$data = $this->Db('mysql_0')->table('mm_user')->asTable('u')
->field('u.*,ui.birthday,ui.info,a.address_info,a.is_default')
->leftJoin('mm_user_info ui on ui.user_id = u.id')
->leftJoin('mm_address a on a.user_id =u.id')
->where()
->order('u.id desc')
->limit(0,10)
->select();
return $data;
}
public function getCount(){
$this->getLastSql();
$data_count = $this->Db('mysql_0')->table('mm_user')->asTable('u')
->field('u.*,ui.birthday,ui.info,a.address_info,a.is_default')
->leftJoin('mm_user_info ui on ui.user_id = u.id')
->leftJoin('mm_address a on a.user_id =u.id')
->where()
->count();
return $data_count;
}
// Insert
$model = new UserModel();
$data =array(
'username'=>$username,
'age'=>$age,
'pwd'=>$password,
'address'=>$address
);
$last_id = $model->Db('mysql_0')->table('mm_user')->insert($data);
// modify
$model = new UserModel();
$arr =array(
'username'=>$username,
'age'=>$age,
'pwd'=>$pwd,
'address'=>$address
);
$where =" id={$id}";
$res=$model->Db('mysql_0')->table('mm_user')->where($where)->update($arr);
// Delete
$model = new UserModel();
$where ="id={$id}";
$res= $model->Db('mysql_0')->table('mm_user')->where($where)->delete();2. newly added Model Of oracle Chain query
// Inquire about
public function getUser(){
$data = $this->Db("oracle_0")->table('"mm_user"')->asTable('u')
->field('u.*,ui."birthday",ui."info",a."address_info",a."is_default"')
->leftJoin('"mm_user_info" ui on ui."user_id" = u."id"')
->leftJoin('"mm_address" a on a."user_id" =u."id"')
->where()
->order('u."id" desc')
->limit(0,2)
->select();
return $data;
}
public function getCount(){
$data_count = $this->Db("oracle_0")->table('"mm_user"')->asTable('u')
->field('u.*,ui."birthday",ui."info",a."address_info",a."is_default"')
->leftJoin('"mm_user_info" ui on ui."user_id" = u."id"')
->leftJoin('"mm_address" a on a."user_id" =u."id"')
->where()
->count();
return $data_count;
}
// Insert
$model = new UserModel();
$data =array(
'id'=>112,
'username'=>$username,
'age'=>$age,
'pwd'=>$password,
'address'=>$address
);
$last_id = $model->Db('mysql_0')->table('"mm_user"')->insert($data);
// modify
$model = new UserModel();
$arr =array(
'username'=>$username,
'age'=>$age,
'pwd'=>$pwd,
'address'=>$address
);
$where ="\"id\"={$id}";
$res=$model->Db('mysql_0')->table('"mm_user"')->where($where)->update($arr);
// Delete
$model = new UserModel();
$where ="\"id\"={$id}";
$res= $model->Db('mysql_0')->table('"mm_user"')->where($where)->delete();
3. Add verifier filter
composer require inhere/php-validate:dev-master
Address :inhere/php-validate - Packagist
The way 2: Inheritance class Validation
Create a new class, And inheritance Inhere\Validate\Validation. For a ( Or a series of related ) Verification of the request , amount to laravel Of Form request validation This is the most complete way to use , You can configure rules , Set field translation , Set custom error messages, etc
use Inhere\Validate\Validation;
class CommonValidate extends Validation
{
# Conduct pre validation processing , return false Then stop verification , But no error messages , Can be called in logic addError Add error message
public function beforeValidate(): bool
{
return true;
}
# Conduct post validation processing , What to do
public function afterValidate(): bool
{
return true;
}
}
class UserValidate extends CommonValidate
{
public function rules(): array
{
return [
// The field must exist and cannot be empty
['tagId,title,userId,freeTime', 'required'],
// 4<= tagId <=567
['tagId', 'size', 'min'=>4, 'max'=>567, 'filter' => 'int'],
// title length >= 40. Note that only one parameter validation is required , No need to add key, Like here. 40
['title', 'min', 40, 'filter' => 'trim'],
// Greater than 0
['freeTime', 'number'],
// With preconditions
['tagId', 'number', 'when' => function($data) {
return isset($data['status']) && $data['status'] > 2;
}],
// Before validation, the conversion will be filtered to int. And it will only indicate that the scene name is 'scene1' Time rule is valid
['userId', 'number', 'on' => 'scene1', 'filter' => 'int'],
['username', 'string', 'on' => 'scene2', 'filter' => 'trim'],
// Use custom regular expressions
['username', 'regexp' ,'/^[a-z]\w{2,12}$/'],
// Custom validator , And specify the message of the current rule
['title', 'custom', 'msg' => '{attr} error msg!' ],
// Use closure validation directly
['status', function($status) {
if (is_int($status) && $status > 3) {
return true;
}
return false;
}],
// Tag fields are secure No need to verify
['createdAt, updatedAt', 'safe'],
];
}
// Define the fields to be verified in different scenarios .
// Functions and rules 'on' similar , Try not to use both at the same time , In order to avoid confusion .
public function scenarios(): array
{
return [
'create' => ['user', 'pwd', 'code'],
'update' => ['user', 'pwd'],
];
}
// Define field translation
public function translates(): array
{
return [
'userId' => ' user Id',
];
}
// Custom validator prompt message , See the default message {@see ErrorMessageTrait::$messages}
public function messages(): array
{
return [
// Specify the message using the verifier name
'required' => '{attr} Are mandatory .',
// You can define messages directly for a rule of a field
'title.required' => 'O, Title is required .are you known?',
];
}
// Add a verifier . A Boolean value must be returned indicating that the validation failed or succeeded
protected function customValidator($title): bool
{
// some logic ...
// $this->getRaw('field'); visit data data
return true; // Or false;
}
}Use
// verification POST data
$v = UserValidate::check($_POST);
// Validation failed
if ($v->isFail()) {
var_dump($v->getErrors());
var_dump($v->firstError());
}
// Verify success ...
$safeData = $v->getSafeData(); // Verified safety data
// $postData = $v->all(); // Raw data
$db->save($safeData);边栏推荐
- HP notebook disable touchpad after mouse is inserted
- How to open a stock account? Is it safe to open a mobile account
- Cesiumjs 2022 ^ source code interpretation [6] - new architecture of modelempirical
- 76 page comprehensive solution 2022 for smart Logistics Park (download attached)
- Analysis of 8253a register
- Qt笔记(七十四)之QLineEdit指定输入类型
- How to use dataant to monitor Apache APIs IX
- Swift 5.0 - creation and use of swift framework
- 35 giant technology companies jointly form the meta universe standard Forum Organization
- To tell you the truth, ThreadLocal is really not an advanced thing
猜你喜欢

The college entrance examination in 2022 is over. Does anyone really think programmers don't need to study after work?

Introduction to machine learning compilation course learning notes lesson 2 tensor program abstraction

Fh6908a negative pole turn off synchronous rectification analog low voltage drop diode control IC chip tsot23-6 ultra low power rectifier 1W power consumption < 100ua static replacement mp6908

深入理解 Jetpack Compose 内核:SlotTable 系统

During telecommuting, the project team punched in the wechat group | solicited papers from the community

Shell multitasking to download video at the same time

Maxpool2d explanation -- Application in arrays and images

Ms17-010 Eternal Blue vulnerability of MSF

New trends of China's national tide development in 2022

Online customer service system code_ H5 customer service_ Docking with official account_ Support app_ Support for multiple languages
随机推荐
如何关闭一个开放的DNS解析器
1175. prime number arrangement / Sword finger offer II 104 Number of permutations
MaxPool2d详解--在数组和图像中的应用
76页智慧物流园区综合解决方案2022(附下载)
Pycharm useful shortcut keys
How to edit special effects in VR panorama? How to display detailed functions?
QQmlApplicationEngine failed to load component qrc:/main. qml:-1 No such file or directory
When we look at the industrial Internet, we always look at it from the opposite of the consumer Internet
New trends of China's national tide development in 2022
Is it safe to open a stock account of Huatai Securities online?
C# /platform:anycpu32bitpreferred 只能与 /t:exe、/t:winexe 和 /t:appcontainerexe 一起使用
How do I open a stock account on my mobile phone? In addition, is it safe to open a mobile account?
SSM integration process (integration configuration, function module development, interface test)
Ctfshow permission maintenance
The girlfriend said: if you want to understand the three MySQL logs, I will let you heiheihei!
composer
During telecommuting, the project team punched in the wechat group | solicited papers from the community
How to close an open DNS resolver
Qlineedit of QT notes (74) specifies the input type
2022-06-30:以下golang代码输出什么?A:0;B:2;C:运行错误。 package main import “fmt“ func main()