Yaf是Yet Another Framework的缩写。Yaf是一个C语言编写的PHP框架。
Yaf的优点
- 用C语言开发的PHP框架,相比原生的PHP,几乎不会带来额外的性能开销。
- 所有的框架类,不需要编译,在PHP启动的时候加载,并常驻内存。
- 更短的内存周转周期,提高内存利用率,降低内存占用率。
- 灵巧的自动加载。支持全局和局部两种加载规则,方便类库共享。
- 高性能的视图引擎。
- 高度灵活可扩展的框架,支持自定义视图引擎,支持插件,支持自定义路由等等。
- 内建多种路由, 可以兼容目前常见的各种路由协议.
- 强大而又高度灵活的配置文件支持. 并支持缓存配置文件, 避免复杂的配置结构带来的性能损失.
- 在框架本身,对危险的操作习惯做了禁止.
- 更快的执行速度, 更少的内存占用.
此 PECL 扩展未与 PHP 捆绑。安装此 PECL 扩展相关的信息可在手册中标题为 PECL 扩展的安装章节中找到。更多信息如新的发行版本、下载、源文件、 维护人员信息及变更日志等,都在此处: https://pecl.php.net/package/yaf.
PECL 扩展的 DLL 当前不可用。参见 在 Windows 上构建章节。
Yaf can be installed from source code by: cd /path/to/yaf-src/ phpize ./configure make sudo make install
运行时配置
这些函数的行为受php.ini中的设置影响。
名字 | 默认 | 可修改范围 | 更新日志 |
---|---|---|---|
yaf.library | its PHP_INI_ALL value | ||
yaf.action_prefer | 0 | its PHP_INI_ALL value | |
yaf.lowcase_path | 0 | its PHP_INI_ALL value | |
yaf.use_spl_autoload | 0 | its PHP_INI_ALL value | |
yaf.forward_limit | 5 | its PHP_INI_ALL value | |
yaf.name_suffix | 1 | its PHP_INI_ALL value | |
yaf.name_separator | its PHP_INI_ALL value | ||
yaf.cache_config | 0 | its PHP_INI_SYSTEM value | |
yaf.environ | product | its PHP_INI_SYSTEM value | |
yaf.use_namespace | 0 | its PHP_INI_SYSTEM value |
范例
Example #1 一个典型的应用目录结构
- index.php - .htaccess + conf |- application.ini //application config - application/ - Bootstrap.php + controllers - Index.php //default controller + views |+ index - index.phtml //view template for default action + modules - library - models - plugins
Example #2 入口文件
顶层目录下的index.php是整个应用的唯一入口,应该把所有请求都重定向到这个文件(在Apache+php_mod模式下可以使用.htaccess)。
<?php define("APPLICATION_PATH", dirname(__FILE__)); $app = new Yaf_Application(APPLICATION_PATH . "/conf/application.ini"); $app->bootstrap() //call bootstrap methods defined in Bootstrap.php ->run(); ?>
Example #3 重写规则
#for apache (.htaccess) RewriteEngine On RewriteCond %{REQUEST_FILENAME} !-f RewriteRule .* index.php #for 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; } } #for lighttpd $HTTP["host"] =~ "(www.)?domain.com$" { url.rewrite = ( "^/(.+)/?$" => "/index.php/$1", ) }
Example #4 应用配置文件
[yaf] ;APPLICATION_PATH is the constant defined in index.php application.directory=APPLICATION_PATH "/application/" ;product section inherit from yaf section [product:yaf] foo=bar
Example #5 默认控制器
<?php class IndexController extends Yaf_Controller_Abstract { /* default action */ public function indexAction() { $this->_view->word = "hello world"; //or // $this->getView() >word = "hello world"; } } ?>
Example #6 默认视图文件
<html> <head> <title>Hello World</title> </head> <body> <?php echo $word;?> </body> </html>
Example #7 运行应用
以上例程的输出类似于:
<html> <head> <title>Hello World</title> </head> <body> hello world </body> </html>
Note:
在yaf@github上有Yaf代码生成器,你也可以用它来生成上面的例子。
Alternative you can generate the example above by using Yaf Code Generator: https://github.com/laruence/php-yaf/tree/master/tools/cg
I success in "application" directory set to: - application/ - Bootstrap.php + modules + Index + controllers - Index.php //default controller + views |+ index - index.phtml //view template for default action - library - models - plugins And Bootstrap.php should be enter at least 3 line like these: <?php class Bootstrap extends Yaf_Bootstrap_Abstract { }
YAF based on the actual case:Hooks、Event、Modules、Plugins、Multiple templates、Multiple languages、SQL Centralized Management... Support Electric Business Platform、OA、ERP、IaaS、PaaS、SaaS、Blog、Cms... Common features required by any platform: User、Acl、Menu... https://github.com/letwang/HookPHP
http://us3.php.net/manual/zh/yaf.tutorials.php Lost default Bootstrap.php <?php /* bootstrap class should be defined under ./application/Bootstrap.php */ class Bootstrap extends Yaf_Bootstrap_Abstract { public function _initConfig(Yaf_Dispatcher $dispatcher) { var_dump(__METHOD__); } public function _initPlugin(Yaf_Dispatcher $dispatcher) { var_dump(__METHOD__); } }
use nginx and php-fpm you can config: location / { try_files $uri $uri/ /index.php$is_args$args; } location ~ \.php$ { fastcgi_pass 127.0.0.1:9000; #fastcgi_pass unix:/tmp/php-cgi.sock; fastcgi_index index.php; fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name; include fastcgi_params; }
the nginx rewrite rule should be: if (!-e $request_filename) { rewrite ^/(.*) /index.php?$1 last; }
When you use nginx & php-fpm config for yaf frame, you must set PATH_INFO support in nginx. Otherwise, it will not work. Demo : location / { try_files $uri $uri/ @path_rw; } #path_info is needed by some php frameworks like yaf/thinkphp location @path_rw { rewrite ^/(.*)$ /index.php/$1 last; } location ~ \.php { fastcgi_pass unix:/dev/shm/php-cgi.sock; fastcgi_index index.php; include fastcgi.conf; fastcgi_split_path_info ^(.+?\.php)(/.+)$; fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name; fastcgi_param SCRIPT_NAME $fastcgi_script_name; fastcgi_param PATH_INFO $fastcgi_path_info; }
应用配置
你需要传递一个config数组或ini配置文件(参见 Yaf_Config_Ini) 给 Yaf_Application::__construct().
Yaf的配置文件会和用户的配置文件合并。区别在于,Yaf的配置文件是以"yaf."或"application."开头,如果两项都存在,则"application."生效。
Example #1 config为数组的例子
<?php $configs = array( "application" => array( "directory" => dirname(__FILE__), "dispatcher" => array( "catchException" => 0, ), "view" => array( "ext" => "phtml", ), ), ); $app = new Yaf_Application($config); ?>
Example #2 config为ini文件的例子
[yaf] yaf.directory = APPLICATION_PATH "/appliation" yaf.dispatcher.catchException = 0 [product : yaf] ; user configuration list here
名字 | 默认 | 更新日志 |
---|---|---|
application.directory | ||
application.ext | "php" | |
application.view.ext | "phtml" | |
application.modules | "index" | |
application.library | application.directory . "/library" | |
application.library.directory | application.directory . "/library" | |
application.library.namespace | "" | |
application.bootstrap | application.directory . "/Bootstrap" . application.ext | |
application.baseUri | "" | |
application.dispatcher.defaultRoute | ||
application.dispatcher.throwException | 1 | |
application.dispatcher.catchException | 0 | |
application.dispatcher.defaultModule | "index" | |
application.dispatcher.defaultController | "index" | |
application.dispatcher.defaultAction | "index" | |
application.system |
这是配置指令的简短说明。
application.directory
string应用程序的目录,包含"controllers", "views", "models", "plugins"等子目录。
Note:
这是唯一一个没有默认值的配置项,你需要手动指定它。
application.ext
stringPHP脚本的扩展名,类的自动加载需要用到它( Yaf_Loader)。
application.view.ext
string视图模板扩展名。
application.modules
string注册的模块列表,以逗号分隔,用于路由处理,特别是当PATH_INFO超过三段的时候,
Yaf需要用它来判断第一段是否是一个模块。
application.library
string本地类库的目录,参见Yaf_Loader 和 yaf.library。
Note:
Yaf2.1.6以后,该配置项也可以是一个数组,当它是数组的时候,类库的路径将尝试使用application.library.directory的值。
application.library.directory
stringAlias of application.library. Introduced in Yaf 2.1.6
application.library.namespace
string逗号分隔的本地类库命名空间前缀。
Yaf2.1.6以后加入
application.bootstrap
stringBootstrap类脚本文件的绝对路径。
application.baseUri
string路由处理中需要忽略的路径前缀。举个例子,请求"/prefix/controller/action"时。如果你将application.baseUri设置为"/prefix",那么只有"/controller/action"会被当做路由路径。
通常不需要设置此值。
application.dispatcher.throwException
bool开启此项,Yaf会在发生错误的地方抛出异常。参见 Yaf_Dispatcher::throwException()。
application.dispatcher.catchException
bool开启此项,如果有未捕获的异常,Yaf将会把它定向到Error controller, Error Action。参见 Yaf_Dispatcher::catchException()。
application.dispatcher.defaultRoute
string默认路由,如果未指定,静态路由会被当做是默认路由,参见: Yaf_Router::addRoute()。
application.dispatcher.defaultModule
string默认模块名,参见 Yaf_Dispatcher::setDefaultModule()。
application.dispatcher.defaultController
string默认控制器名,参见 Yaf_Dispatcher::setDefaultController()。
application.dispatcher.defaultAction
string默认动作名,参见 Yaf_Dispatcher::setDefaultAction()。
application.system
string在application.ini中设置Yaf运行时配置,如: application.system.lowcase_path
Note:
仅有PHP_INI_ALL配置项能这样设置