对于Yaf的应用, 都应该遵循类似下面的目录结构.
例 3.1. 一个典型的目录结构
+ public
|- index.php //入口文件
|- .htaccess //重写规则
|+ css
|+ img
|+ js
+ conf
|- application.ini //配置文件
+ application
|+ controllers
|- Index.php //默认控制器
|+ views
|+ index //控制器
|- index.phtml //默认视图
|+ modules //其他模块
|+ library //本地类库
|+ models //model目录
|+ plugins //插件目录
例 3.2. 一个经典的入口文件public/index.php
<?php
define("APP_PATH", realpath(dirname(__FILE__) . '/../')); /* 指向public的上一级 */
$app = new Yaf_Application(APP_PATH . "/conf/application.ini");
$app->run();
除非我们使用基于query string的路由协议(Yaf_Route_Simple, Yaf_Route_Supervar), 否则我们就需要使用WebServer提供的Rewrite规则, 把所有这个应用的请求, 都定向到上面提到的入口文件.
例 3.3. Apache的Rewrite (httpd.conf)
#.htaccess, 当然也可以写在httpd.conf
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule .* index.php
例 3.4. Nginx的Rewrite (nginx.conf)
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;
}
}
例 3.5. Lighttpd的Rewrite (lighttpd.conf)
$HTTP["host"] =~ "(www.)?domain.com$" {
url.rewrite = (
"^/(.+)/?$" => "/index.php/$1",
)
}
例 3.6. SAE的Rewrite (config.yaml)
name: your_app_name
version: 1
handle:
- rewrite: if(!is_dir() && !is_file() && path ~ "^(.*)$" ) goto "/index.php"
![]() |
注意 |
|---|---|
| 每种Server要启用Rewrite都需要特别设置, 如果对此有疑问.. RTFM |
在Yaf中, 配置文件支持继承, 支持分节. 并对PHP的常量进行支持. 你不用担心配置文件太大造成解析性能问题, 因为Yaf会在第一个运行的时候载入配置文件, 把格式化后的内容保持在内存中. 直到配置文件有了修改, 才会再次载入.
例 3.7. 一个简单的配置文件application/conf/application.ini
[product]
;支持直接写PHP中的已定义常量
application.directory=APP_PATH "/application/"
对于默认模块, 控制器的目录是在application目录下的controllers目录下, Action的命名规则是"名字+Action"
例 3.8. 默认控制器application/controllers/Index.php
<?php
class IndexController extends Yaf_Controller_Abstract {
public function indexAction() {//默认Action
$this->getView()->assign("content", "Hello World");
}
}
?>