Yaf(Yet Another Freamwork)框架是中国国内教父级php程序员鸟哥的主力作品之一, 在php.net有鸟哥(惠新宸)的大名, 可谓国之骄傲。Yaf的运行并非堆积php类文件或者写一些其他php层面上的封装,而是采取了php扩展模式,随php常驻内存。想要学习Yaf, 需要先在环境上入手, 安装(编译)必要的扩展文件。
安装下载Yaf
先在Yaf的github页面右上角找到.zip文件包地址 或者到PHP官网上面的yaf扩展下载页面里下载。
然后将地址复制到控制台里用wget命令拿到
以下是.zip包的编译安装案例:
wget https://github.com/laruence/yaf/archive/master.zip unzip master.zip cd yaf-master
设$PHP_BIN为php的bin目录,自己找一下这个目录放哪里啦
$PHP_BIN/phpize ./configure --with-php-config=$PHP_BIN/php-config make make install
.tgz包的编译安装案例:
从php官网下载到.tgz的包放在~目录下
tar -zxvf yaf-2.3.5.tgz cd yaf-2.3.5 # 假设/phpstudy/server/php/bin为php的bin目录 /phpstudy/server/php/bin/phpize ./configure --with-php-config=/phpstudy/server/php/bin/php-config make make install
在php.ini里面填上yaf.so加载Yaf扩展
extension=yaf.so
启用命名空间的方法也是在php.ini当中加两行
yaf.use_namespace=1 yaf.environ="product"
其中environ这个配置项和application.ini的配置标准相关,product这个名字也是自己根据情况起的名字。
Yaf的配置
nginx的配置
server { listen 80; server_name maxtv5.wkwkk.com; root "/home/web/www.wkwkk.com/public"; if (!-e $request_filename) { rewrite ^/(.*) /index.php/$1 last; } location / { index index.html index.htm index.php; #autoindex on; } location ~ \.php(.*)$ { fastcgi_pass 127.0.0.1:9000; fastcgi_index index.php; fastcgi_split_path_info ^((?U).+\.php)(/?.+)$; fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name; fastcgi_param PATH_INFO $fastcgi_path_info; fastcgi_param PATH_TRANSLATED $document_root; include fastcgi_params; } }
注意: 如果发现重写规则失败,找不到文件,安装的是nginx较高(1.5+)版本的服务器,则把 / 符号改为 ?,即:
rewrite ^/(.*) /index.php?$1 last;
别忘了重启生效
Apache的配置
.htaccess正文里写入以下代码:
RewriteEngine On RewriteCond %{REQUEST_FILENAME} !-f RewriteRule .* index.php
Yaf的代码初始化模板
很多同学不会或者懒得使用鸟哥的代码初始化工具, 那么可以粘贴以下代码来用
先建立三个目录: application conf public
在application当中建立目录: controllers library models modules plugins views
application/Bootstrap.php文件代码:
<?php /** * @name Bootstrap * @author root * @desc 所有在Bootstrap类中, 以_init开头的方法, 都会被Yaf调用, * @see http://www.php.net/manual/en/class.yaf-bootstrap-abstract.php * 这些方法, 都接受一个参数:Yaf_Dispatcher $dispatcher * 调用的次序, 和申明的次序相同 */ class Bootstrap extends Yaf\Bootstrap_Abstract { public function _initConfig() { Yaf\Registry::set('config', Yaf\Application::app()->getConfig()); } public function _initPlugin(Yaf\Dispatcher $dispatcher) { // 注册一个插件 // $objSamplePlugin = new SamplePlugin(); // $dispatcher->registerPlugin($objSamplePlugin); } public function _initRoute(Yaf\Dispatcher $dispatcher) { $dispatcher->getInstance()->getRouter()->addConfig(Yaf\Registry::get('config')->routes); } public function _initView(Yaf\Dispatcher $dispatcher) { // 在这里注册自己的view控制器,例如smarty,firekylin // $dispatcher->getInstance()->disableView(); } public function _initSession(Yaf\Dispatcher $dispatcher) { // 初始化Session } public function _initCommonFunction() { // $directory = Yaf\Application::app()->getConfig()->application->directory; // Yaf\Loader::import($directory . '/Constant.php'); // Yaf\Loader::import($directory . '/Common.php'); } }
application/controllers/Index.php的代码
<?php class IndexController extends Yaf\Controller_Abstract { public function indexAction() { // echo 'do something'; return false; } }
application/plugins/Sample.php的代码:
<?php /** * @name SamplePlugin * @desc Yaf定义了如下的6个Hook,插件之间的执行顺序是先进先Call * @see http://www.php.net/manual/en/class.yaf-plugin-abstract.php * @author root */ class SamplePlugin extends Yaf\Plugin_Abstract { public function routerStartup(Yaf_Request_Abstract $request, Yaf_Response_Abstract $response) { } public function routerShutdown(Yaf_Request_Abstract $request, Yaf_Response_Abstract $response) { } public function dispatchLoopStartup(Yaf_Request_Abstract $request, Yaf_Response_Abstract $response) { } public function preDispatch(Yaf_Request_Abstract $request, Yaf_Response_Abstract $response) { } public function postDispatch(Yaf_Request_Abstract $request, Yaf_Response_Abstract $response) { } public function dispatchLoopShutdown(Yaf_Request_Abstract $request, Yaf_Response_Abstract $response) { } }
conf/application.ini的代码
[common] application.directory=APP_PATH "/application/" ;application.dispatcher.catchException=TRUE application.modules="Index" [routes] routes.article.type="regex" routes.article.match="#^/article/([0-9]+).html$#" routes.article.route.module="Index" routes.article.route.controller="Index" routes.article.route.action="Todo" routes.article.map.1="id" routes.simple.type = "simple" routes.simple.module = "m" routes.simple.controller = "c" routes.simple.action = "a" [product : common:routes] [develop : common:routes]
public/index.php 也就是入口文件的代码:
<?php /** * Yaf框架基本入口 * @author caster 20160806 */ header('Content-type:text/html;charset=utf-8'); // 开发模式 ini_set('display_errors',1); error_reporting(E_ALL); date_default_timezone_set('PRC'); // 定义入口文件 define('APP_PATH', __DIR__ . '/../'); // 指向public上一级 define('ROOT_PATH',__DIR__); $app = new Yaf\Application(APP_PATH . '/conf/application.ini'); $app->bootstrap()->run();