快速开始

在开始演示之前,请先确保如下环境已经成功安装.

创建演示项目

  1. $ composer create-project fastd/fastd-demo

确定以上步骤安装完毕后,运行 PHP 内置 WEB 服务器:

  1. $ php -S localhost:9876 -t web

PHP 内置 WEB 服务器可前往官网: http://php.net/manual/zh/features.commandline.webserver.php

访问: http://localhost:9876

  1. curl -i http://localhost:9876

响应

  1. HTTP/1.1 200
  2. Host: 127.0.0.1:8888
  3. Date: Fri, 29 Dec 2017 10:32:53 +0800
  4. Connection: close
  5. X-Powered-By: PHP/7.1.11-1+ubuntu16.04.1+deb.sury.org+1
  6. Content-Type: application/json; charset=UTF-8
  7. {"msg":"welcome fastd"}

恭喜你,已经成功安装 fastd,是不是比想象中的简单。😆

使用 swoole

fastd 最大的一个特性之一,就是天生支持 swoole,可以让你的应用程序 "飞" 起来。

  1. $ php bin/server start

访问监听的地址,结果与PHP 内置 Web 服务器结果保持一致,但是速度明显提升了。

输出:

  1. HTTP/1.1 200 OK
  2. Content-Type: application/json; charset=UTF-8
  3. Server: swoole-http-server
  4. Connection: keep-alive
  5. Date: Fri, 29 Dec 2017 02:33:25 GMT
  6. Content-Length: 23
  7. {"msg":"welcome fastd"}

就是这么简单,什么都不用修改,就直接支持 swoole 执行了。

值得注意的是: composer 在 cygwin 环境下,创建软连接失败,需要手动调整脚本。

  1. dir=$(d=${0%[/\\]*}; cd "$d"; cd '../vendor/fastd/fastd' && pwd)
  2. # See if we are running in Cygwin by checking for cygpath program
  3. if command -v 'cygpath' >/dev/null 2>&1; then
  4. # Cygwin paths start with /cygdrive/ which will break windows PHP,
  5. # so we need to translate the dir path to windows format. However
  6. # we could be using cygwin PHP which does not require this, so we
  7. # test if the path to PHP starts with /cygdrive/ rather than /usr/bin
  8. if [[ $(which php) == /cygdrive/* ]]; then
  9. dir=$(cygpath -m "$dir");
  10. fi
  11. fi
  12. dir=$(echo $dir | sed 's/ /\ /g')
  13. "${dir}/server" "$@"

此时此刻别慌,只需要将脚本内容拷贝到执行脚本中即可。

  1. bin/console => vendor/fastd/fastd/bin/console
  2. bin/server => vendor/fastd/fastd/bin/server
  3. bin/process => vendor/fastd/fastd/bin/process
  4. bin/client => vendor/fastd/fastd/bin/client