一、 前言
多年前就听说Yaf,C语言编写的PHP框架,作者为鸟哥,php界牛人!
今天就来入个门。
首先附上几个有用的链接:
* Yaf文档-鸟哥主页 http://www.laruence.com/manual/
* Yaf文档-PHP官网http://php.net/manual/zh/book.yaf.php
* Yaf扩展地址http://pecl.php.net/package/yaf
二、环境配置
以windows为例。
- 第一步,确定自己所用php版本和类型(ZTS,TS)。
命令行下输入php -v
,查看自己所用的php版本。(别问我怎么打开命令行)
版本7.0.1,NTS - 第二步,从pecl下载对应的dll。
由于是windows系统,点击 DLL。
目前笔者使用的操作系统为32位的,所以要选 X86,由上一步可知,我们需要选择7.0版本,NTS。 - 第三步,复制yaf扩展到php扩展目录,修改
php.ini
上图为解压后的文件列表。
在php.ini中增加一行 extension=php_yaf.dll - 第四步,确认是否安装成功。
命令行中输入php -m
,如果没有错误提示,且输出中包含yaf
,说明扩展安装成功。
三、Hello World
1.创建脚手架
根据文档,建立如下目录结构。
public目录下的index.php是入口文件,为了安全,web服务器的目录应设为public这个目录。conf目录下的application.ini是配置文件。application/controllers目录下的Index.php是控制器,application/views目录下的为视图模板。
2.搭建一个虚拟主机
打开C:\Windows\System32\drivers\etc 目录下的hosts文件,加入一行 127.0.0.1 yafapp.cc。
nginx配置文件作如配置:
server {
listen 80;
server_name yaf.cc yaf.cc;
root "D:/phpStudy/www/yafapp/public";
location / {
index index.html index.htm index.php;
#autoindex on;
try_files $uri $uri/ /index.php$uri;
}
location ~ \.php(.*)$ {
fastcgi_pass 127.0.0.1:9000;
fastcgi_index index.php;
fastcgi_split_path_info ^((?U).+\.php)(/?.+)$;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
fastcgi_param PATH_INFO $fastcgi_path_info;
fastcgi_param PATH_TRANSLATED $document_root$fastcgi_path_info;
include fastcgi_params;
}
}
重启服务器。
3. 编程测试
- 入口文件index.php内容如下
<?php
define('APP_PATH', realpath(dirname(__DIR__)));
$app = new Yaf_Application(APP_PATH . "/conf/application.ini");
$app->run();
首先,定义应用根目录,然后指定应用的配置文件创建一个应用,最后,运行该应用。
- application.ini内容如下
[product]
application.directory=APP_PATH "/application/"
application.ext=php
根据文档,application.directory 是唯一一个没有默认值的配置项,必须手动指定。
- 控制器内容如下(controllers/Index.php):
<?php
class IndexController extends Yaf_Controller_Abstract
{
public function indexAction()
{
$this->getView()->assign("content", "Hello World");
}
}
控制器类名默认以Controller
结尾,而保存的文件名则为类名去掉Controller
后的字符串。动作的名称默认以Action
结尾,访问的时候则不需要加上Action
。
- 视图模板内容如下(views/index/index.php)
<html>
<head>
<title> My first yaf app</title>
</head>
<body>
<h1><?php echo $content;?></h1>
</body>
</html>
views下的index目录是控制器名的小写形式,模板名称则与action的小写名称对应。
- 运行之
恭喜!!成功入门!!