Skip to content

相关介绍

zmisgod edited this page Feb 25, 2018 · 1 revision

相关命令

操作 介绍
php index.php start 启动
php index.php stop 关闭
php index.php reload 重启
php index.php status 查看服务器状态
php index.php --help 显示帮助命令

使用

如果想要使用多端口监听tcpudp,需要在配置文件中将multi_port设置为true,并在tcp或者udpopen选项中设置为true开启。

server port open
http 9519 true
tcp 9520 true
udp 9521 true

Web路由

路由 对应文件 方法名
http://127.0.0.1:9519/index/benchmark App\Controller\Http\IndexController.php benchmark()

其中,并且类文件的方法需要为公开的方法(public function)并且类需要继承App\Http\Controller

HTML模版引擎

支持HTML模版,需要将控制器继承Core\Framework\ViewController,然后在App目录下新建View 因为本框架现仅支持2级路由,所以View的下一级目录则为控制器的小写名称,然后此目录下则为某方法对应的view视图

路由 对应视图文件
http://127.0.0.1:9519/demo/template App\Http\View\demo\template.php

如果对应的视图找不到则会显示404页面,404页面默认在Public目录下

Mysql

内置mysqli,并实现相应的断线重连。使用方法:

use Core\Uti\DB\Mysqli;

Mysqli::getInstance()->query('show tables')->fetchall();

# debug
Mysqli::getInstance()->setDebug(true)->query('show tables')->printDebug();

Swoole相关内置函数使用

详情见App\Controller\DemoController这个类,demo包括下列方法的使用

  • swoole_task
  • swoole_timer_tick
  • swoole_timer_clear
  • swoole_timer_after
  • tcp_client
  • udp_client
  • mysqli
  • get/post参数接收

配置文件

配置文件在Config文件夹中。获取配置文件示例:
$serverConfig = Config::getInstance()->getConfig('config.server');
其中,config为Config下的config.php文件

生产与开发配置分离

为了线上配置与开发配置便于管理,防止开发的配置文件用于生产环境,您可以在根目录创建一个.env文件,在其中写相应的配置,然后再在Config\config.php中使用Core\Uti\Tools\Tools::getInstance()->getEnv('KEY')获取KEY的配置(请看.env_sample示例)。

Wechat 微信

支持EasyWechat

静态文件

静态文件在Public目录下(暂时需要配合nginx处理静态资源)

Nginx配置域名

server {
    listen       80;
    server_name your.server.name;
    root to/your/path/TinySwoole/Public/;
    
    if ( $uri = "/" ) {
       rewrite ^(.*)$ /index last;
    }
    
    location / {
        proxy_http_version 1.1;
        proxy_set_header Connection "keep-alive";
        proxy_set_header X-Real-IP $remote_addr;
        # 判断文件是否存在,如果不存在,代理给Swoole
        if (!-e $request_filename){
            proxy_pass http://127.0.0.1:9519;
        }
    }
}