這篇講講yaf的配置文件,首先上我的配置代碼:
[common] ;必選配置 ;application.directory String 應用的絕對目錄路徑 ;可選配置 ;名稱 值類型 默認值 說明 ;application.ext String php PHP腳本的擴展名 ;application.bootstrap String Bootstrapplication.php Bootstrap路徑(絕對路徑) ;application.library String application.directory + "/library" 本地(自身)類庫的絕對目錄地址 ;application.baseUri String NULL 在路由中, 需要忽略的路徑前綴, 一般不需要設置, Yaf會自動判斷. ;application.dispatcher.defaultModule String index 默認的模塊 ;application.dispatcher.throwException Bool True 在出錯的時候, 是否拋出異常 ;application.dispatcher.catchException Bool False 是否使用默認的異常捕獲Controller, 如果開啟, 在有未捕獲的異常的時候, 控制權會交給ErrorController的errorAction方法, 可以通過$request->getException()獲得此異常對象 ;application.dispatcher.defaultController String index 默認的控制器 ;application.dispatcher.defaultAction String index 默認的動作 ;application.view.ext String phtml 視圖模板擴展名 ;application.modules String Index 聲明存在的模塊名, 請注意, 如果你要定義這個值, 一定要定義Index Module ;application.system.* String * 通過這個屬性, 可以修改yaf的runtime configure, 比如application.system.lowcase_path, 但是請注意只有PHP_INI_ALL的配置項才可以在這里被修改, 此選項從2.2.0開始引入 ;--------------------------------------------------------------------------------------------------------------------- ;PHP.ini文件可以配置的項 ;選項名稱 默認值 可修改范圍 更新記錄 ;yaf.environ product PHP_INI_ALL 環境名稱, 當用INI作為Yaf的配置文件時, 這個指明了Yaf將要在INI配置中讀取的節的名字 ;yaf.library NULL PHP_INI_ALL 全局類庫的目錄路徑 ;yaf.cache_config 0 PHP_INI_SYSTEM 是否緩存配置文件(只針對INI配置文件生效), 打開此選項可在復雜配置的情況下提高性能 ;yaf.name_suffix 1 PHP_INI_ALL 在處理Controller, Action, Plugin, Model的時候, 類名中關鍵信息是否是后綴式, 比如UserModel, 而在前綴模式下則是ModelUser ;yaf.name_separator "" PHP_INI_ALL 在處理Controller, Action, Plugin, Model的時候, 前綴和名字之間的分隔符, 默認為空, 也就是UserPlugin, 加入設置為"_", 則判斷的依據就會變成:"User_Plugin", 這個主要是為了兼容ST已有的命名規范 ;yaf.forward_limit 5 PHP_INI_ALL forward最大嵌套深度 ;yaf.use_namespace 0 PHP_INI_SYSTEM 開啟的情況下, Yaf將會使用命名空間方式注冊自己的類, 比如Yaf_Application將會變成Yaf\Application ;yaf.use_spl_autoload 0 PHP_INI_ALL 開啟的情況下, Yaf在加載不成功的情況下, 會繼續讓PHP的自動加載函數加載, 從性能考慮, 除非特殊情況, 否則保持這個選項關閉 ;application application.directory = APPLICATION_PATH"/application" application.library = APPLICATION_PATH"/library" ;如果配置模塊,index為填項 application.modules = index,admin,user application.dispatcher.catchException = True application.dispatcher.defaultModule = index application.dispatcher.defaultController = index application.dispatcher.defaultAction = index application.view.ext=phtml ;框架里都沒有做unix嵌套子連接的判斷,全部是基於ip的 ;database ;程序實現的mysql負載均衡,沒有failover線上環境需謹慎 database.mysql_cache_enable=True database.mysql_log_error=True database.pdotype="mysql" ;master database.master.host = "127.0.0.1" database.master.port = 3306 database.master.user="root" database.master.password="" database.master.database="test" ;slaves database.slaves.1.host = "127.0.0.1" database.slaves.1.port="3306" database.slaves.1.user = "yaftest" database.slaves.1.password = "123456" database.slaves.1.database="test" ;cache database.cache.caching_system="CacheMemcache" database.cache.cache_exclude_table="" ;600秒 database.cache.lifetime=600 database.cache.prefix="cye_" database.cache.object_cache_enable=True ;smarty smarty.template_dir = APPLICATION_PATH"/application/views" smarty.compile_dir = APPLICATION_PATH"/cache/smarty/compile" smarty.cache_dir = APPLICATION_PATH"/cache/smarty/cache" smarty.caching = FALSE smarty.force_compile = FALSE smarty.compile_check = True ;routes 最后定義的最優先 ;index.php/test/--->index.php/index/index/index/name/test routes.index.type = "regex" routes.index.match = "#^/([a-zA-Z]+)/?#" routes.index.route.module = Index routes.index.route.controller = Index routes.index.route.action = Index routes.index.map.1 = name ;自定義路由 ;順序很重要 ;index.php/list/test/abc--->index.php/index/index/index/name/test/value/abc routes.regex.type="regex" routes.regex.match="#^list/([^/]*)/([^/]*)#" routes.regex.route.controller=Index routes.regex.route.action=action routes.regex.map.1=name routes.regex.map.2=value ;index.php?m=m&c=c&a=a&test=555--->index.php/m/c/a/test/555 routes.simple.type="simple" routes.simple.module=m routes.simple.controller=c routes.simple.action=a ;index.php?r=m/c/a&test=555--->index.php/m/c/a/test/555 routes.supervar.type="supervar" routes.supervar.varname=r ;默認的Yaf_Application ;將會讀取此節配置 ;補充路由定義 ;index.php/product/test/555--->index.php/index/product/info/name/test/value/555 routes.rewrite.type="rewrite" routes.rewrite.match="/product/:name/:value" routes.rewrite.route.controller=product routes.rewrite.route.action=info ;resource resource.js=APPLICATION_PATH"/public/js" resource.css=APPLICATION_PATH"/public/css" resource.img=APPLICATION_PATH"/public/img" ;mongodb ;redis ;httpsqs ;memcache memcache.1.host="192.168.1.188" memcache.1.port=11211 ;smtp smtp.server="smtp.sample.com" smtp.ssl=True smtp.username="sample@sample.com" smtp.password="password" smtp.helo="www.sample.com" ;customer customer.debug = True customer.benchmark = True [product : common] ;smarty.caching = TRUE ;smarty.force_compile = FALSE ;smarty.compile_check = TRUE ;cookie cookie.prefix="cookie_prefix" cookie.domain="dev.com" ;session ;使用系統的 ;session.save_handler = files ;session.save_path = "E:/wamp/tmp/test" ;帶有tcp的為memcache,不帶的為memcached,后面可以加?persistent=1&weight=2等參數 session.save_handler = memcache ;session.save_path = "tcp://192.168.1.188:11211" ;這里的memcache是自己實現的,也可以是redis,mysql等擴展要自己寫 session.save_path.1.host = "192.168.1.188" session.save_path.1.port = 11211 ;使用系統的時候單位是分鍾,使用memcached的時候單位是秒,數據是基於秒的 session.expire = 1800 session.name = "phpsessname" session.is_json = False session.cookie_lifetime=1800 session.cookie_path="/" session.cookie_domain = "dev.com" session.cookie_secure = False session.cookie_httponly = False
以;開頭的是注釋語句,前面兩段是主要配置信息以及說明,整個配置文件其實只要第一句,配置了application的directory就可以運行了。
[common]括起來的是配置段名稱,可以通過:繼承,例如[product:common]。
整個配置文件會被解析存儲為一個config對象,通過.分割的子對象,這個對象可以通過Yaf_Application::app()->getConfig()方法獲取,例如:
$config = Yaf_Application::app()->getConfig();$session = $config->session;然后$session->is_json就可以取到False值。這里很簡單就不多說了。
下面看看bootstrap文件的寫法,這一步其實不是必須的,但是實用的項目肯定需要這個文件,在里面可以初始化一些必須的操作。
<?php class Bootstrap extends Yaf_Bootstrap_Abstract { protected $config; //注冊配置對象到config,可以全局使用 public function _initConfig(Yaf_Dispatcher $dispatcher) {} //注冊自定義類庫的命名空間 public function _initLocalName() {} //配置是否報錯 public function _initError(Yaf_Dispatcher $dispatcher) {} //注冊插件 public function _initPlugin(Yaf_Dispatcher $dispatcher) {} //配置路由 public function _initRoute(Yaf_Dispatcher $dispatcher) {} //配置memcache,類似的mongodb,redis,httpsqs等如果需要也可以統一配置 //如果在程序里面使用的話,盡量放到Yaf_Registry里 public function _initMemcache(Yaf_Dispatcher $dispatcher) {} //配置session public function _initSession(Yaf_Dispatcher $dispatcher) {} //配置數據庫 //這里做的數據庫負載均衡 public function _initDatabase(Yaf_Dispatcher $dispatcher) {} //配置郵件服務器 public function _initMailer(Yaf_Dispatcher $dispatcher) {} //配置smarty模板引擎 public function _initSmarty(Yaf_Dispatcher $dispatcher) {} }
以上例舉了可以通過bootstrap初始化的功能,按每個方法分解
//注冊配置對象到config,可以全局使用 public function _initConfig(Yaf_Dispatcher $dispatcher) { $this->config = Yaf_Application::app()->getConfig(); Yaf_Registry::set('config', $this->config); }
//注冊自定義類庫的命名空間 public function _initLocalName() { Yaf_Loader::getInstance()->registerLocalNamespace(array( 'Smarty', 'Foo', )); }
//配置是否報錯 public function _initError(Yaf_Dispatcher $dispatcher) { if ($this->config->customer->debug){ define('DEBUG_MODE', false); ini_set('display_errors', 'On'); }else{ define('DEBUG_MODE', false); ini_set('display_errors', 'Off'); } }
//注冊插件 public function _initPlugin(Yaf_Dispatcher $dispatcher) { if (isset($this->config->customer->benchmark) && $this->config->customer->benchmark == true){ $benchmark = new BenchmarkPlugin(); $dispatcher->registerPlugin($benchmark); } }
//配置路由 public function _initRoute(Yaf_Dispatcher $dispatcher) { //配置文件添加 $routes = $this->config->routes; if (!empty($routes)){ $router = $dispatcher->getRouter(); $router->addConfig($routes); } //語句添加 $route = new Yaf_Route_Rewrite( "/product/list/:id/:name", array( "controller" => "product", "action" => "info", ) ); $router->addRoute('dummy', $route); }
//配置memcache,類似的mongodb,redis,httpsqs等如果需要也可以統一配置 //如果在程序里面使用的話,盡量放到Yaf_Registry里 public function _initMemcache(Yaf_Dispatcher $dispatcher) { if (!empty($this->config->memcache)){ $memcache_client = Bootstrap::memFaction($this->config->memcache); Yaf_Registry::set('memcache', $memcache_client); } }
//根據配置文件返回memcache實例 public static function memFaction($configArray){ $flag = extension_loaded("memcached"); if($flag){ $memA = array(); $memcache_client = new memcached(); } else { $memcache_client = new memcache(); } foreach($configArray as $mem_server){ if($flag){ $memA[] = array($mem_server['host'] , $mem_server['port']); }else{ $re = $memcache_client->addServer($mem_server['host'] , $mem_server['port']); if($re == true){ $Result = true; } } } if($flag){ $Result = $memcache_client->addServers($memA); } if(!$Result){ exit('can not connect memcache server'); } return $memcache_client; }
public function _initSession(Yaf_Dispatcher $dispatcher) { if (!empty($this->config->session)) { $session = $this->config->session; session_set_cookie_params( $session->cookie_lifetime, $session->cookie_path, $session->cookie_domain, $session->cookie_secure, $session->cookie_httponly ); session_name($session->name); if(!empty($session->save_handler)){ switch($session->save_handler){ case 'memcache': //使用系統的 if(is_string($session->save_path)){ if(strpos($session->save_path, "://")){ session_module_name('memcache'); }else{ session_module_name('memcached'); } session_save_path($session->save_path); session_cache_expire($session->expire/60); }else{ //使用自定義的配置不同 SessionMemcache::init($session->save_path,$session->expire,$session->is_json); } break; case 'files': if($session->save_path){ if(!file_exists($session->save_path)){ mkdir($session->save_path, true); if(!file_exists($session->save_path)){ exit('session_path is not exits'); } } session_module_name('files'); session_save_path($session->save_path); session_cache_expire($session->expire/60); } break; default: break; } } } }
//配置數據庫 //這里做的數據庫負載均衡 public function _initDatabase(Yaf_Dispatcher $dispatcher) { $servers = array(); $database = $this->config->database; $servers[] = $database->master->toArray(); $slaves = $database->slaves; if (!empty($slaves)) { foreach ($slaves as $key => $slave_server) { $slavesA[] = $slave_server->toArray(); } $servers[] = $slavesA[array_rand($slavesA)]; if(count($servers) != 2) { exit("slaves config wrong"); } } Yaf_Registry::set('database', $servers); if (isset($database->mysql_cache_enable) && $database->mysql_cache_enable && !defined('MYSQL_CACHE_ENABLE')) { define('MYSQL_CACHE_ENABLE', true); } if (isset($database->mysql_log_error) && $database->mysql_log_error && !defined('MYSQL_LOG_ERROR')) { define('MYSQL_LOG_ERROR', true); } if (isset($database->pdotype) && $database->pdotype && !defined('PDO_TYPE')) { define('PDO_TYPE', $database->pdotype); } // $pdo = new PDO(PDO_TYPE . ":dbname={$servers[0]['database']};host={$servers[0]['host']};port={$servers[0]['port']}", $servers[0]['user'], $servers[0]['password']); // $pdo->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_WARNING); // $pdo->setAttribute(PDO::ATTR_CASE, PDO::CASE_LOWER); // $fpdo = new FluentPDO($pdo); // $query = $fpdo->from('article'); // print_r($query->fetchAll()); // exit; //以前的 //Yaf_Loader::import(APPLICATION_PATH . '/library/Db/Db.php'); //Yaf_Loader::import(APPLICATION_PATH . '/library/Db/DbQuery.php'); }
//配置smarty模板引擎 public function _initSmarty(Yaf_Dispatcher $dispatcher) { $smarty = new Smarty_Adapter(null, $this->config->smarty); $smarty->registerFunction('function', 'truncate', array('Tools', 'truncate')); //注冊到smarty Yaf_Registry::set("smarty", $smarty); //直接替換原始模板對象 //$dispatcher->setView($smarty); }
這些方法執行順序是從上到下了,那個方法先定義就先執行,里面有點復雜session,db后面在做介紹