yaf框架流程二
这篇讲讲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..0开始引入
;--------------------------------------------------------------------------------------------------------------------- ;PHP.ini文件可以配置的项
;选项名称 默认值 可修改范围 更新记录
;yaf.environ product PHP_INI_ALL 环境名称, 当用INI作为Yaf的配置文件时, 这个指明了Yaf将要在INI配置中读取的节的名字
;yaf.library NULL PHP_INI_ALL 全局类库的目录路径
;yaf.cache_config PHP_INI_SYSTEM 是否缓存配置文件(只针对INI配置文件生效), 打开此选项可在复杂配置的情况下提高性能
;yaf.name_suffix 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 PHP_INI_ALL forward最大嵌套深度
;yaf.use_namespace PHP_INI_SYSTEM 开启的情况下, Yaf将会使用命名空间方式注册自己的类, 比如Yaf_Application将会变成Yaf\Application
;yaf.use_spl_autoload 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 =
database.master.user="root"
database.master.password=""
database.master.database="test"
;slaves
database.slaves..host = "127.0.0.1"
database.slaves..port=""
database.slaves..user = "yaftest"
database.slaves..password = ""
database.slaves..database="test"
;cache
database.cache.caching_system="CacheMemcache"
database.cache.cache_exclude_table=""
;600秒
database.cache.lifetime=
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. = 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.=name
routes.regex.map.=value ;index.php?m=m&c=c&a=a&test=--->index.php/m/c/a/test/
routes.simple.type="simple"
routes.simple.module=m
routes.simple.controller=c
routes.simple.action=a ;index.php?r=m/c/a&test=--->index.php/m/c/a/test/
routes.supervar.type="supervar"
routes.supervar.varname=r
;默认的Yaf_Application
;将会读取此节配置 ;补充路由定义
;index.php/product/test/--->index.php/index/product/info/name/test/value/
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..host="192.168.1.188"
memcache..port= ;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=&weight=2等参数
session.save_handler = memcache
;session.save_path = "tcp://192.168.1.188:11211"
;这里的memcache是自己实现的,也可以是redis,mysql等扩展要自己写
session.save_path..host = "192.168.1.188"
session.save_path..port = ;使用系统的时候单位是分钟,使用memcached的时候单位是秒,数据是基于秒的
session.expire =
session.name = "phpsessname"
session.is_json = False session.cookie_lifetime=
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/);
}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/);
}
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) != )
{
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后面在做介绍
yaf框架流程二的更多相关文章
- yaf框架流程四
在前面的章节,在bootstrap里添加了一个benchmark插件,简单介绍下yaf的插件机制:http://yaf.laruence.com/manual/yaf.plugin.html Yaf定 ...
- yaf框架流程三
路由的原理请看http://yaf.laruence.com/manual/yaf.routes.html这个链接 要点:路由的顺序是堆栈模式的,及最后添加的路由规则最优先.由上两篇可知,定义的第一条 ...
- Yaf框架下类的自动加载
前面两篇博客分别讲述了PHP自带的类加载和composer中类的自动加载,其实Yaf框架也实现了基于PSR0和PSR4的类的自动加载.根据我对Yaf下类的自动加载方式的理解写下这篇博客.由于接触Yaf ...
- Struts2框架学习(二) Action
Struts2框架学习(二) Action Struts2框架中的Action类是一个单独的javabean对象.不像Struts1中还要去继承HttpServlet,耦合度减小了. 1,流程 拦截器 ...
- php 安装yaf扩展和yaf框架
一.安装yaf扩展(windows安装) 1.查看你电脑安装的开发环境(phpinfo()的信息),查找 "Zend Extension Build"和"PHP Exte ...
- MVC系列——MVC源码学习:打造自己的MVC框架(二:附源码)
前言:上篇介绍了下 MVC5 的核心原理,整篇文章比较偏理论,所以相对比较枯燥.今天就来根据上篇的理论一步一步进行实践,通过自己写的一个简易MVC框架逐步理解,相信通过这一篇的实践,你会对MVC有一个 ...
- JavaScript 框架设计(二)
JavaScript 高级框架设计 (二) 上一篇,JavaScript高级框架设计(一)我们 实现了对tag标签的选择 下来我们实现对id的选择,即id选择器. 我们将上一篇的get命名为getTa ...
- yaf框架学习笔记
1.yaf框架支持简单的试图引擎,并且支持用户自定义视图引擎,比如smarty. 2.Yaf_Request_Http::getQuery ,Yaf_Request_Http::getQuery ( ...
- 【原创】NIO框架入门(二):服务端基于MINA2的UDP双向通信Demo演示
前言 NIO框架的流行,使得开发大并发.高性能的互联网服务端成为可能.这其中最流行的无非就是MINA和Netty了,MINA目前的主要版本是MINA2.而Netty的主要版本是Netty3和Netty ...
随机推荐
- mvc5 _ViewStart.cshtml 模板页如何定义
1._Viewstart.cshtml是一个在呈现View文件的时候的启动文件,会在所有View(.cshtml)被执行之前执行,主要用于一些不方便或不能在母版(_Layout.cshtml)中进行的 ...
- LINUX输入输出与文件
1 文件描述符 内核为每个进程维护一个已打开文件的记录表(实现为结构体数组),文件描述符是一个较小的正整数(0-1023)(结构体数组下标),它代表记录表的一项,通过文件描述符和一组基于文件描述符的文 ...
- jquery 中$.post获取MVC Controller中JsonResult返回包含LIst<Model>类型的子List<Model>的高级使用方法
比如JsonResult中返回return Json(models);的models结构如下: models返回含有四个集合的序列,每个集合的序列中又包含一个子集合序列“Child”. 问题是如果我们 ...
- Centos环境下部署游戏服务器-SSH
在这个*nix大行其道的年代,SSH绝对值得你拥有,虽说它的语法真的很简单,可用途相当大.比如说你在登山,突然公司一个电话告诉你服务器出问题了,在登山的过程中,肯定不能背个几公斤的笔记本吧,这个时候只 ...
- springMVC找不到JS等文件
应用springMVC时 JS等文件找不到错误 应用springMVC时如果配置URL映射时如下配置 <servlet> <servlet-name>appSe ...
- Can't find bundle for base name ClientMessages, locale zh_CN
这是个关于JAVA国际化方面的语音包的问题. 提示这个错误信息就是说找不到代码里写的配置文件. 我这个错误发生在导入一个已经存在的项目时发生的. 解决办法:将配置文件*.properties所在的文件 ...
- 微信jssdk uploadImage 巨坑
//解决IOS无法上传的坑 if (localId.indexOf("wxlocalresource") != -1) { localId = localId.replace(&q ...
- poj2265
斜坐标系,注意找规律.说明在代码里. e- ?(x):-(x))<eps) ][] = { { , }, { -, }, { -, }, { , - }, { , - }, { , } ...
- nandflash学习1——导致nandflash反转的原因【转】
转自:http://blog.csdn.net/gaosentao/article/details/7711311 Bit Flip/Bit Flipping/Bit-Flip/Bit twiddli ...
- dojo 六 使用query dojo/query
要使用query,就要引入dojo/query包.query可以根据Dom里节点的标签名.id名.class名来检索一个或多个节点.---------------------------------- ...