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.phpYaf的代碼初始化模板
很多同學不會或者嬾得使用鳥哥的代碼初始化工具, 那麽可以粘貼以下代碼來用
先建立三個目錄: 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();