学习资料
Yaf(Yet Another Framework)用户手册
http://www.laruence.com/manual/index.html
laruence/yaf
https://github.com/laruence/yaf
php yaf框架开发扩展实践
http://www.01happy.com/php-yaf-ext-preface/
http://www.php.net/manual/zh/book.yaf.php
特点
1、基于c语言开发;
2、以php扩展的形式安装(.so,.dll)
3、没有封装数据库操作的ORM
安装yaf扩展
扩展下载网址:http://pecl.php.net/package/yaf
windows请下载对应版本的php_yaf.dll,并复制到ext/ 目录。
其它版本请下载源码进行编译。
验证是否安装成功,使用phpinfo查看,如显示出yaf即成功:
或者在命令行使用php -m,若成功安装,可以看到yaf扩展。
参考源码里面的test进行搭建demo。
项目结构
─application 应用目录
│ ├─controllers 控制器目录
│ │ ├ Error.php
│ │ └ Index.php
│ ├─library 库目录
│ ├─models 模型目录
│ │ └ Sample.php
│ ├─plugins 插件目录
│ │ └ Sample.php
│ ├─views 视图目录
│ │ ├─error
│ │ └─index
│ └── Bootstrap.php
├─conf 配置文件目录
│ └─ application.ini 配置文件
├─public 静态文件目录
└── index.php 入口文件
入口文件
index.php
<?php
define("APP_PATH", dirname(__FILE__));
/*
$config = array(
"application" => array(
"directory" => APP_PATH . "/application",
),
);
$app = new yaf_application($config);
*/
//加载配置文件
$app = new Yaf_Application(APP_PATH . "/conf/application.ini");
$app->bootstrap() //call bootstrap methods defined in Bootstrap.php
->run();
配置文件
conf/application.ini
[common]
; 支持直接写PHP中的已定义常量
application.directory = APP_PATH "/application"
application.dispatcher.catchException = TRUE
application.view.ext = html
; 数据库配置
resources.database.params.driver = "pdo_mysql"
resources.database.params.hostname = "127.0.0.1"
resources.database.params.port = 3306
resources.database.params.database = "database"
resources.database.params.username = "username"
resources.database.params.password = "password"
resources.database.params.charset = "UTF8"
resources.database.params.driver_options.1002 = "SET NAMES utf8"
[product : common]
; 生产环境配置
[test : common]
; 测试环境配置
[develop : common]
; 开发环境配置
resources.database.params.hostname = "127.0.0.1"
resources.database.params.database = "database"
resources.database.params.username = "username"
resources.database.params.password = "password"
控制器
application/controllers
在Yaf中, 默认的模块/控制器/动作, 都是以Index命名的, 当然,这是可通过配置文件修改的。
对于默认模块, 控制器的目录是在application目录下的controllers目录下。
示例:application/controllers/Index.php
<?php
/**
* @name IndexController
* @author {&$AUTHOR&}
* @desc 默认控制器
* @see http://www.php.net/manual/en/class.yaf-controller-abstract.php
*/
class IndexController extends Yaf_Controller_Abstract {
/**
* 默认动作
* Yaf支持直接把Yaf_Request_Abstract::getParam()得到的同名参数作为Action的形参
* 对于如下的例子, 当访问http://yourhost/{&$APP_NAME&}/index/index/index/name/{&$AUTHOR&} 的时候, 你就会发现不同
*/
public function indexAction($name = "Stranger") {
//1. fetch query
$get = $this->getRequest()->getQuery("get", "default value");
//2. fetch model
$model = new SampleModel();
//3. assign
$this->getView()->assign("content", $model->selectSample());
$this->getView()->assign("name", $name);
//4. render by Yaf, 如果这里返回FALSE, Yaf将不会调用自动视图引擎Render模板
return TRUE;
}
public function testAction() {
var_dump(YAF_VERSION );exit;
}
}
浏览器上访问:
http://localhost/demo/yaf/index.php/index
http://localhost/demo/yaf/index.php/index/test
视图文件
application/views
Yaf支持简单的视图引擎, 并且支持用户自定义自己的视图引擎, 比如Smarty。对于默认模块, 视图文件的路径是在application目录下的views目录中以小写的action名的目录中.
一个默认Action的视图application/views/index/index.html:
<?php
echo $content, " I am ", $name;
?>
模型
yaf/application/models
yaf没有封装数据库操作的ORM。这里是示例的一个简单模型,无实际意义,仅做展示。
示例:yaf/application/models/Sample.php
<?php
/**
* @name SampleModel
* @desc sample数据获取类, 可以访问数据库,文件,其它系统等
* @author {&$AUTHOR&}
*/
class SampleModel {
public function __construct() {
}
public function selectSample() {
return 'Hello World!';
}
public function insertSample($arrInfo) {
return true;
}
}
yaf框架封装类库
虽然yaf性能很快,但是缺少诸如表单、数据库操作等类库的封装,在开发上不免带来不便。可以自行集成第三方类库。
(http://git.oschina.net/iceup/yaf-ext)