首页 > 新闻动态 >  

新闻动态
NEWS

开端yaf之旅

添加时间:2013-7-25 点击量:

目次布局



+ public  //网站根目次

- index.php //进口文件
- .htaccess //重写规矩
+ conf
|- application.ini //设备文件
application/
+ controllers
- Index.php //默认把握器
+ views
|+ index //把握器
- index.phtml //默认视图
+ modules //其他模块
- library //组件目次
- models //model目次
- plugins //插件目次





进口文件





进口文件是所有恳求的进口, 一般都借助于rewrite规矩, 把所有的恳求都重定向到这个进口文件.


一个经典的进口文件public/index.php



<?php

define(APP_PATH, realpathdirname__FILE__) . /../)); / 指向public的上一级 /
¥app = new Yaf_Application(APP_PATH . /conf/application.ini);
¥app->run();





重写规矩





除非我们应用基于query string的路由和谈(Yaf_Route_Simple, Yaf_Route_Supervar), 不然我们就须要应用WebServer供给的Rewrite规矩, 把所有这个应用的恳求, 都定向到上方提到的进口文件.


批改.htaccess文件


Nginx的Rewrite (nginx.conf)



server {

listen
80;
server_name yaf.demo.com;
root document_root;
index index.php index.html index.htm;

if (!-e ¥request_filename) {
rewrite
^/(.) /index.php/¥1 last;
}
}





设备文件





在Yaf中, 设备文件支撑持续, 支撑分节. 并对PHP的常量进行支撑. 你不消愁闷设备文件太大造成解析机能题目, 因为Yaf会在第一个运行的时辰载入设备文件, 把格局化后的内容对峙在内存中. 直到设备文件有了批改, 才会再次载入.


一个简单的设备文件application/conf/application.ini



[common]

application.directory
= APP_PATH /application
application.dispatcher.catchException
= 0
application.dispatcher.throwException
= 0
application.view.ext
= phtml
[product : common]
;enable the error controller
application.dispatcher.catchException
=1





把握器





在Yaf中, 默认的模块/把握器/动作, 都是以Index定名的, 当然,这是可经由过程设备文件批改的.


对于默认模块, 把握器的目次是在application目次下的controllers目次下, Action的定名规矩是名字+Action


默认把握器application/controllers/Index.php


 

<?php

class IndexController extends Yaf_Controller_Abstract {
public function indexAction() {//默认Action
¥this->getView()->assign(content, Hello World);
}
}
?>





视图文件





Yaf支撑简单的视图引擎, 并且支撑用户自定义本身的视图引擎, 比如Smarty.


对于默认模块, 视图文件的路径是在application目次下的views目次中以小写的action名的目次中.


一个默认Action的视图application/views/index/index.phtml


 

<html>

<head>
<title>Hello yaf</title>
</head>
<body>
<?php echo ¥content;?>
</body>
</html>


然后在浏览器输入nginx.conf设置的servername,yaf.demo.com。是不是出来了??




表 4.2. Yaf可选设备项









名称值类型默认值申明
application.ext彼此相爱,却不要让爱成了束缚:不如让它成为涌动的大海,两岸乃是你们的灵魂。互斟满杯,却不要同饮一杯。相赠面包,却不要共食一个。一起歌舞欢喜,却依然各自独立,相互交心,却不是让对方收藏。因为唯有生命之手,方能收容你们的心。站在一起却不要过于靠近。—— 纪伯伦《先知》
分享到: