LNMP(Linux+Nginx+Mysql+PHP)环境下安装yaf框架并编写一个MVC例子
1、下载yaf源码包yaf-2.2.9.tar.gz,并将其上传到服务器指定的位置,然后解压并安装:
[root@Slave1pcsrc]#tar-xvfyaf-2.2.9.tar.gz
进入解压后的目录:
[root@Slave1pcsrc]#cdyaf-2.2.9
一次执行:
[root@Slave1pcyaf-2.2.9]#/usr/local/php/bin/phpize
[root@Slave1pcyaf-2.2.9]#./configure--with-php-config=/usr/local/php/bin/php-config
[root@Slave1pcyaf-2.2.9]#make&&makeinstall
2、查看编译后的文件:
[root@Slave1pc~]#ll/usr/local/php/lib/php/extensions/no-debug-non-zts-20121212/yaf.so
-rwxr-xr-x1rootroot771610Jul818:13/usr/local/php/lib/php/extensions/no-debug-non-zts-20121212/yaf.so
//可以看到yaf.sophp扩展也已经帮我们编译好了
3、配置php.ini
[root@Slave1pc~]#vim/usr/local/php/lib/php.ini
extension=yaf.so//然后在php.ini中载入yaf.so
4、重启php-fpm
[root@Slave1pc~]#servicephp-fpmrestart
Gracefullyshuttingdownphp-fpm.done
Startingphp-fpmdone
5、查看phpinfo()
3、利用Yaf自带的快速代码生成工具yaf_code_generator生成代码:
1)下载yaf工具包,浏览https://github.com/laruence/php-yaf,下载源码包php-yaf-yaf-2.2.9.tar.gz,并将其上传到服务器指定位置,然后解压:
[root@Slave1pcsrc]#tar-xvfphp-yaf-yaf-2.2.9.tar.gz
然后进入解压后的目录:
[root@Slave1pcsrc]#cdphp-yaf-yaf-2.2.9
然后进入tools/cg目录下:
[root@Slave1pcphp-yaf-yaf-2.2.9]#cdtools/cg/
然后执行(app是生成的目录名):
[root@Slave1pccg]#/usr/local/php/bin/phpyaf_cgapp
执行以上代码,将在cg/output/目录生成一份yaf的骨架代码app
2)将生成的app项目骨架代码复制到nginx的html(网站根目录)目录下:
[root@Slave1pc~]#cp-r/usr/src/php-yaf-yaf-2.2.9/tools/cg/output/app/usr/local/nginx/html/
3)一个典型的yaf应用的目录结构:
+public
|-index.php//入口文件
|-.htaccess//重写规则
|+css
|+img
|+js
+conf
|-application.ini//配置文件
+application
|+controllers
|-Index.php//默认控制器
|+views
|+index//控制器
|-index.phtml//默认视图
|+modules//其他模块
|+library//本地类库
|+models//model目录
|+plugins//插件目录
入口文件:是所有请求的入口,一般都借助于rewrite规则,把所有的请求都重定向到这个入口文件,一个经典的入口文件如下:
[root@Slave1pcapp]#vimindex.php
<?php
define('APPLICATION_PATH',dirname(__FILE__));
$application=newYaf_Application(APPLICATION_PATH."/conf/application.ini");
$application->bootstrap()->run();
?>
配置文件:在Yaf中,配置文件支持继承,支持分节。并对PHP的常量进行支持,不用担心配置文件太大造成解析性能问题,因为Yaf会在第一个运行的时候载入配置文件,把格式化后的内容保持在内存中,直到配置文件有了修改,才会再次载入,一个简单的配置文件:
[root@Slave1pcconf]#vimapplication.ini
[common]
application.directory=APPLICATION_PATH"/application"
application.dispatcher.catchException=TRUE
[product:common]
重写规则:除非我们使用基于querystring的路由协议(Yaf_Route_Simple,Yaf_Route_Supervar),否则我们就需要使用WebServer提供的Rewrite规则,把所有这个应用的请求,都定向到上面提到的入口文件;
Nginx的Rewrite(nginx.conf),按如下修改文件:
[root@Slave1pcconf]#vimnginx.conf
location/{
roothtml/app;
#roothtml;
indexindex.htmlindex.htmindex.php;
}
location~\.php${
roothtml/app;
#roothtml;
fastcgi_pass127.0.0.1:9000;
fastcgi_indexindex.php;
fastcgi_paramSCRIPT_FILENAME/scripts$fastcgi_script_name;
includefastcgi_params;
}
if(!-e$request_filename){
#rewrite^/(.*)/index.php/$1last;//这样不行
rewrite^/(.*)/index.php?$1last;
}
修改完配置文件nginx.conf后记得重启:
[root@Slave1pcconf]#servicenginxrestart
nginx:theconfigurationfile/usr/local/nginx/conf/nginx.confsyntaxisok
nginx:configurationfile/usr/local/nginx/conf/nginx.conftestissuccessful
Stoppingnginx:[OK]
Startingnginx:[OK]
4)一个基于Yaf框架的MVC的简单例子:
控制器(controllers):在Yaf中,默认的模块/控制器/动作,都是以Index命名的,当然,这是可通过配置文件修改的。对于默认模块,控制器的目录是在application目录下的controllers目录下,Action的命名规则是"名字+Action";
[root@Slave1pccontrollers]#vimTest.php
<?php
class TestController extends Yaf_Controller_Abstract {
public function testAction() {
//1. fetch query
$get = $this->getRequest()->getQuery("get", "default value");
//2. fetch model
$model = new TestModel();
//3. assign
$this->getView()->assign("lists", $model->selectName());
return TRUE;
}
}
?>
模型(models):数据获取类,可以访问数据库,文件,其它系统等
[root@Slave1pcmodels]#vimTest.php
<?php
/**
* @models数据获取类, 可以访问数据库,文件,其它系统等
*/
class TestModel {
public function __construct() {
}
public function selectName() {
$host="localhost";
$user="root";
$passwd="20082009";
$db="heat";
$query = "select name,content from heat1 order by name";
$mysqli = new mysqli($host,$user,$passwd) or dir("Unable to connect to mysql!");
$mysqli->select_db($db);
$mysqli->query("SET NAMES 'utf8'");//
$result = $mysqli->query($query);
while($rows = $result->fetch_array()){
$lists[] = $rows;
//$lists = iconv("utf-8","gbk",$lists);//编码转换函数iconv只能接受字符串参数
}
$result->free(); //释放资源
$mysqli->close();//关闭数据库连接
$result = $lists;
//数组编码解决方法
$result = eval('return '.iconv('utf-8','gbk',var_export($result,true)).';');
return $result;
}
}
?>
其中数据库heat中表heat1的内容为:
视图(views):Yaf支持简单的视图引擎,并且支持用户自定义自己的视图引擎,比如Smarty。
对于默认模块,视图文件的路径是在application目录下的views目录中以小写的action名的目录中;
[root@Slave1pctest]#vimtest.phtml
<?php
//循环输出数组内容
foreach($lists as $list){
echo $list['name'], " please see: ", $list['content'];
echo "<br />";
}
?>
在浏览器输入:http://172.16.2.33/index.php/Test/test,即显示如下页面:
更多相关文章
- Oracle:从SQL文件批量导入数据
- linux清空日志文件内容 比如log日志
- sql2008中如何收缩数据库日志文件
- sql2000 数据库文件突然丢失
- 关于NavicatPremium导入CSV文件乱码的问题
- 解决缺少sql头文件编译错误
- mysql通过复制data文件夹进行数据迁移
- MySQL 笔记(三)由 txt 文件导入数据
- 直接的文件备份和用sqlserver的备份有什么区别