Yaf

Yaf is a PHP framework similar to zend framework, which is written in c and built as PHP extension.

Yaf是一个用PHP扩展形式提供的PHP开发框架, 相比于一般的PHP框架, 它更快. 它提供了Bootstrap, 路由, 分发, 视图, 插件, 是个一个全功能的PHP框架.

you can refer to Yaf Manual(中文版, 中文版翻译滞后, 最新版本的文档, 还是请参看php.net上的英文版) for more informations.

Pecl: yaf
Source Codes: Svn
Author's blog: 风雪之隅

1 Install

Compile Yaf in Linux

$/path/to/phpize
$./configure --with-php-config=/path/to/php-config/
$make && make install

2 Tutorial

==layout==: A classic Application directory layout:

- index.php //入口文件
- .htaccess //重写规则    
+ conf
  |- application.ini //配置文件   
- application/
  - Bootstrap.php   //入口启动文件
  + controllers
     - Index.php //默认控制器
  + views    
     |+ index   //控制器
        - index.phtml //默认视图
  + modules //其他模块
  - library
  - models  //model目录
  - plugins //插件目录

index.php

index.php in the top directory is the only way in of the application, you should rewrite all request to it(you can use .htaccess in Apache+php_mod)

<?php
define("APP_PATH",  dirname(__FILE__));
 
$app  = new Yaf_Application(APP_PATH . "/conf/application.ini");
$app->bootstrap() //call bootstrap methods defined in Bootstrap.php
 ->run();

rewrite rules

Apache

#.htaccess, 当然也可以写在httpd.conf
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule .* index.php

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;
  }
}

Lighttpd

$HTTP["host"] =~ "(www.)?domain.com$" {
  url.rewrite = (
     "^/(.+)/?$"  => "/index.php/$1",
  )
}

application.ini

application.ini is the application config file

[product]
;支持直接写PHP中的已定义常量
application.directory=APP_PATH "/application/" 

default controller

In Yaf, the default controller is named IndexController?:

<?php
class IndexController extends Yaf_Controller_Abstract {
   public function indexAction() {//默认Action
   }
}
?>

view script

The view script for default controller and default action is in the application/views/index/index.phtml, Yaf provides a simple view engineer called Yaf_View_Simple?, which supported the view template written by PHP.

<html>
 <head>
   <title>Hello World</title>
 </head>
 <body>
    Hellow World!
 </body>
</htlm>

Run the Applicatioin

http://www.yourhostname.com/application/index.php

3 Classes

it provide following Classes

NAME COMMENT
Yaf_Application?
Yaf_Dispatcher?
Yaf_Bootstrap_Abstract?
Yaf_Plugin_Abstract?
Yaf_Router?
Yaf_Route_Static?
Yaf_Route_Simple?
Yaf_Route_Supervar?
Yaf_Route_Rewrite?
Yaf_Route_Regex?
Yaf_Route_Map?
Yaf_Config_Ini?
Yaf_Config_Simple?
Yaf_Controller_Abstract?
Yaf_Action_Abstract?
Yaf_Request_Http?
Yaf_Request_Simple?
Yaf_Exception?
Yaf_Exception?
Yaf_View_Simple?;
Yaf_Response_Abstract?
Yaf_Response_Simple?
Yaf_Response_Http?