README.md 2.9 KB
Newer Older
lzc828's avatar
readme  
lzc828 已提交
1 2 3 4 5 6 7 8 9 10 11 12 13
# One - 一个极简的基于swoole常驻内存框架

> 支持在fpm下运行

## 安装&运行

```shell
composer create-project lizhichao/one-app app
cd app
php App/swoole.php 
```

[详细文档地址](https://www.kancloud.cn/vic-one/php-one/826876)
lzc828's avatar
init  
lzc828 已提交
14

lzc828's avatar
readme  
lzc828 已提交
15
[使用列子-DEMO](https://github.com/lizhichao/one-demo)
lzc828's avatar
init  
lzc828 已提交
16

lzc828's avatar
readme  
lzc828 已提交
17
QQ交流群: 731475644
lzc828's avatar
init  
lzc828 已提交
18

lzc828's avatar
readme  
lzc828 已提交
19 20 21 22 23 24 25 26
## 路由

```php
Router::get('/', \App\Controllers\IndexController::class . '@index');

// 带参数路由
Router::get('/user/{id}', \App\Controllers\IndexController::class . '@user');

lzc828's avatar
readme  
lzc828 已提交
27 28 29 30 31 32
// 路由分组 
Router::group(['namespace'=>'App\\Test\\WebSocket'],function (){
    Router::set('ws','/a','TestController@abc'); // websocket 路由
    Router::set('ws','/b','TestController@bbb'); // websocket 路由
});

lzc828's avatar
readme  
lzc828 已提交
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
```

## orm 模型

### 定义模型
```php
namespace App\Model;

use One\Database\Mysql\Model;

class User extends Model
{
    CONST TABLE = 'users';

    public function articles()
    {
        return $this->hasMany('id',Article::class,'user_id');
    }
}
```

### 使用

在fpm下数据库连接为单列,  
在swoole模式下数据库连接自动切换为连接池

```php
// 查询一条记录
User::find(1);

// 关联查询
User::whereIn('id',[1,2,3])->with('articles')->findAll();

// 更新
lzc828's avatar
lzc828 已提交
67
user::where('id',1)->update(['name' => 'aaa']);
lzc828's avatar
init  
lzc828 已提交
68 69

```
lzc828's avatar
demo  
lzc828 已提交
70

lzc828's avatar
readme  
lzc828 已提交
71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142
## 日志
```php
Log::debug('abc');
Log::debug(['12,312']);

// 等级|时间|requestId|文件路径:行号|内容
// DEBUG|2018-11-23 15:01:26|WelxrVb5UEoFaDrQ59XnQ|/Controllers/IndexController.php:12|abc
// DEBUG|2018-11-23 15:01:26|WelxrVb5UEoFaDrQ59XnQ|/Controllers/IndexController.php:13|["12,312"]
```

## 缓存
```php
Cache::set('a',1);

// ccc 在 tag1标签下
Cache::set('ccc',3,['tag1']);

// 刷新 tag1 下的所有缓存
Cache::flush('tag1');
```

## session
``` 
$this->session()->get('aaa');
$this->session()->set('aaa',123);

```
        
## 添加一个webSocket服务监听

需要开启什么事件就 重写父类相应事件

```php

[
    'port' => 8082,
    'action' =>  \App\Server\AppWsServer::class, //类名 作为server自带继承 WsServer ;作为监听添加继承 \One\Swoole\Listener\Ws
    'type' => SWOOLE_SOCK_TCP,
    'ip' => '0.0.0.0',
    'set' => [
        'open_http_protocol' => false,
        'open_websocket_protocol' => true
    ]
]

```

## 添加一个TCP服务监听

需要开启什么事件就 重写父类相应事件

```php

[
    'port' => 8083,
    'protocol' => \One\Protocol\Text::class, // 协议
    'action' => \App\Test\MixPro\TcpPort::class, //类名 作为server自带继承 TcpServer ;作为监听添加继承 \One\Swoole\Listener\Tcp
    'type' => SWOOLE_SOCK_TCP,
    'ip' => '0.0.0.0',
    'set' => [
        'open_http_protocol' => false,
        'open_websocket_protocol' => false
    ]
]

```

[详细文档地址](https://www.kancloud.cn/vic-one/php-one/826876)

[使用列子-DEMO](https://github.com/lizhichao/one-demo)

QQ交流群: 731475644