快速开始
在开始演示之前,请先确保如下环境已经成功安装.
创建演示项目
$ composer create-project fastd/fastd-demo
确定以上步骤安装完毕后,运行 PHP 内置 WEB 服务器:
$ php -S localhost:9876 -t web
PHP 内置 WEB 服务器可前往官网: http://php.net/manual/zh/features.commandline.webserver.php
curl -i http://localhost:9876
响应
HTTP/1.1 200
Host: 127.0.0.1:8888
Date: Fri, 29 Dec 2017 10:32:53 +0800
Connection: close
X-Powered-By: PHP/7.1.11-1+ubuntu16.04.1+deb.sury.org+1
Content-Type: application/json; charset=UTF-8
{"msg":"welcome fastd"}
恭喜你,已经成功安装 fastd,是不是比想象中的简单。😆
使用 swoole
fastd 最大的一个特性之一,就是天生支持 swoole,可以让你的应用程序 "飞" 起来。
$ php bin/server start
访问监听的地址,结果与PHP 内置 Web 服务器结果保持一致,但是速度明显提升了。
输出:
HTTP/1.1 200 OK
Content-Type: application/json; charset=UTF-8
Server: swoole-http-server
Connection: keep-alive
Date: Fri, 29 Dec 2017 02:33:25 GMT
Content-Length: 23
{"msg":"welcome fastd"}
就是这么简单,什么都不用修改,就直接支持 swoole 执行了。
值得注意的是: composer 在 cygwin 环境下,创建软连接失败,需要手动调整脚本。
dir=$(d=${0%[/\\]*}; cd "$d"; cd '../vendor/fastd/fastd' && pwd)
# See if we are running in Cygwin by checking for cygpath program
if command -v 'cygpath' >/dev/null 2>&1; then
# Cygwin paths start with /cygdrive/ which will break windows PHP,
# so we need to translate the dir path to windows format. However
# we could be using cygwin PHP which does not require this, so we
# test if the path to PHP starts with /cygdrive/ rather than /usr/bin
if [[ $(which php) == /cygdrive/* ]]; then
dir=$(cygpath -m "$dir");
fi
fi
dir=$(echo $dir | sed 's/ /\ /g')
"${dir}/server" "$@"
此时此刻别慌,只需要将脚本内容拷贝到执行脚本中即可。
bin/console => vendor/fastd/fastd/bin/console
bin/server => vendor/fastd/fastd/bin/server
bin/process => vendor/fastd/fastd/bin/process
bin/client => vendor/fastd/fastd/bin/client