基于macos系统,PHP版本7.0.19
yaf介绍
Yaf = yet another framework
鸟哥用C语言编写的一个PHP扩展,致力于解决框架影响性能这一经典问题
Yaf框架 = 最不像框架的框架 = 对高性能有要求时选择
安装yaf扩展
brew install php70-yaf --build-from-source
根据自己的php版本下载对应的扩展,如你的版本是php5.6那么就通过brew下载php56-yaf
在终端运行php -m,能看到yaf,说明扩展安装成功(或打印phpinfo())
➜ ~ php -m | grep yaf
yaf
建立demo项目
用yaf的代码生成器cg快速生成一个demo project
下载https://github.com/laruence/yaf后解压
cd tools
cd cg
php yaf_cg test
cd output
cp -R test ~/test
设置虚拟主机
- 修改server_name域名,root项目根目录
server {
listen 80;
server_name yaf.com;
location / {
root ~/test;
index index.html index.htm index.php;
try_files $uri $uri/ /index.php;
}
error_page 404 /404.html;
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root ~/test;
}
location ~ \.php {
root ~/test;
fastcgi_pass 127.0.0.1:9000;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
include fastcgi_params;
}
}
- 修改hosts文件,加入下面的内容
127.0.0.1 yaf.com
这样就可以通过访问域名yaf.com访问到demo项目了
测试
访问yaf.com,能看到Hello World! I am Stranger,说明安装配置成功

补充:安装方式已经变了
原文的三处安装方式在今天都失效或不推荐了,但 Yaf 扩展本身仍在维护,所以这篇笔记仍然有用,只是安装段需要替换。
为什么 brew install php70-yaf 已经不工作
homebrew/php 这个 tap 早在 2018 年就被 Homebrew 官方废弃并删除(理由是 PHP 各版本的生命周期节奏和 brew 的滚动更新冲突)。原文的 brew tap homebrew/php 现在会直接报 Error: homebrew/php was deprecated。任何形如 php70-xxx、php56-xxx 的 formula 都随之消失了。
今天的装法:pecl
# 前置依赖,pecl 编译扩展需要
brew install autoconf pkg-config
# 找到当前 PHP 对应的 pecl
php -v
which pecl # 一般随 PHP 一起装好
pecl install yaf
装完把扩展写进 ini:
# 先确认 ini 目录
php --ini | grep "Scan for additional"
echo "extension=yaf.so" > /usr/local/etc/php/8.3/conf.d/99-yaf.ini
路径里的 8.3 换成你自己的版本号,或者直接用 php --ini 打印出来的目录。写完后 php -m | grep yaf 能看到 yaf 就是成功了。
常见编译失败:configure: error: C compiler cannot create executables 通常是 Xcode command line tools 没装或没更新,xcode-select --install 解决;php.h not found 说明缺少 php-config,需要 brew install php 提供的开发头文件。
原文用 PECL 装的是 3.x 之前的版本
今天 pecl 上的 Yaf 是 3.x。3.x 相对 2.x 有破坏性变化,最典型的是配置项 yaf.use_namespace 在 3.x 中已废弃——3.x 的类名本来就是 Yaf\Application 这种带命名空间的。原文里如果出现 yaf.use_namespace=1,在 3.x 下会提示未知配置项,直接删掉即可。
yaf_cg 生成器
原文提到的 yaf_cg(Yaf 官方的项目骨架生成器)没有消失,但它不在 PECL 包里,而在源码仓库的 tools/cg 目录下:
git clone https://github.com/laruence/yaf.git
cd yaf/tools/cg
./yaf_cg myapp
生成的目录结构就是原文后面几篇笔记(module、config、MVC)里用的那套。
本文整理于 2017 年,安装方式基于 homebrew/php tap + PHP 7.0。上述补充按 PHP 8.x + Yaf 3.x 书写。