基础
- 官方手册
- Yaf是一个C语言编写的PHP框架
- 依赖SPL和PCRE扩展,一般PHP默认启用
优点
- 性能开销小
- 框架类在启动PHP时加载,常驻内存
- 高性能的视图引擎
- 可自定义视图引擎,支持插件,支持自定义路由
Hello World
结构
+ public
|- index.php //入口文件
|- .htaccess //重写规则
|+ css
|+ img
|+ js
+ conf
|- application.ini //配置文件
+ application
|- Bootstrap.php
|+ controllers
|- Index.php //默认控制器
|+ views
|+ index //控制器
|- index.phtml //默认视图
|+ modules //其他模块
|+ library //本地类库
|+ models //model目录
|+ plugins //插件目录
入口文件
<?php
define("APP_PATH", realpath(dirname(__FILE__) . '/../'));
$app = new Yaf_Application(APP_PATH . "/conf/application.ini");
$app
->bootstrap()
->run();
重写规则
- 每种server都要特别设置重写规则
- 重写所有请求到入口文件
- nginx为例
server {
listen ****;
server_name domain.com;
root document_root;
index index.php index.html index.htm;
if (!-e $request_filename) {
rewrite ^/(.*) /index.php/$1 last;
}
}
配置文件
- 可以直接使用php中已定义的常量
- 配置文件支持继承, 支持分节
- 默认节是product
[product]
application.directory=APP_PATH "/application/"
引导程序
- yaf提供的一个全局配置的入口, 可以做很多全局自定义的工作
class Bootstrap extends Yaf_Bootstrap_Abstract{
}
默认控制器
- 默认控制器为IndexController
- 默认Action为IndexAction
<?php
class IndexController extends Yaf_Controller_Abstract {
public function indexAction() {
$this->getView()->assign("content", "Hello World");
}
}
默认视图文件
- yaf支持简单的视图引擎
- 默认视图文件为views/index/index.phtml
<html>
<head>
<title>Hello World</title>
</head>
<body&g