===说明===
<?php
define('IN_DEBUG',true);
IN_DEBUG ? error_reporting(E_ALL) : error_reporting(0);
$environ=ini_get('yaf.environ'); //默认是product
define("APPLICATION_PATH", realpath(dirname(__FILE__).'/../') );
$app = new Yaf_Application(APPLICATION_PATH."/conf/application.{$environ}.ini");
$app->bootstrap()->run();
*入口文件注意3个地方:
**yaf.environ, 该配置属于PHP_INI_SYSTEM,仅在php.ini配置生效。可以配置成你想要的值,默认是product。当用ini作配置文件时,它会使用该值为名的节
**Yaf_Application的构造函数有2个参数。第一个参数是配置项,可以传一个数组,或者一个ini文件的地址。第2个参数是个字符串,传值后如果使用ini文件作为配置,将使用该值对应的节作为配置,默认为yaf.environ的值
**在new Yaf_Application("conf.ini")之后,Yaf_Application::run之前,如果调用Yaf_Application::bootstrap()( 即代码中的$app->bootstrap()->run() ),将会依次执行Bootstrap.php中所有以_init开头的方法
[yaf]
application.directory=APPLICATION_PATH "/application"
application.dispatcher.throwException=TRUE
application.dispatcher.catchException=TRUE
application.dispatcher.defaultController="site"
application.dispatcher.defaultAction = "index"
application.view.ext="php"
[common : yaf]
site.title=" 我的官网"
site.discountMin=100
site.limitPerOrder=30
*Yaf支持用php和ini两种文件格式的配置文件,其中php文件需返回一个数组,ini文件传入后也会被转换成一个对应的多维数组(实际是一个对象)
*如果使用使用ini作为配置文件,site.title="我的官网" 等效于 array('site'=>array('title'=>"我的官网") )
*代码中[yaf]定义了一个名叫yaf的配置节,[common : yaf]定义了一个叫common的配置节,它继承yaf配置节。
*如果打开yaf.cache_config,ini配置文件将会被缓存
//Base_Ctrl所有controller的父类
class Base_Ctrl extends Yaf_Controller_Abstract {
public $layoutfile = 'column_main';
public $main_view='main';
public $pageTitle=''; //页面title
//等等等等。。。。这些变量可以在view里面通过$C来直接传输和调用
public function render($phtml,$params=array(),$return=false) {
$paramsExtra = array_merge($params,array('actionName'=>$this->getActionId(),'controllerName'=>$this->getControllerId()));
$paramsExtra['C'] = $this;//把controller实例传给view, 方便传一些参数和方法过去
$html1 = $this->render($phtml, $paramsExtra);//先渲染action自身的view
if ( !empty($this->layoutfile) ) {
//如果需要layout, 则渲染layout
$html1 = $this->getView()->render(PATH_TPL.'/layout/'.$this->layoutfile.'.php', array_merge($paramsExtra,array('content'=>$html1)) );
}
//最后渲染最外层的公共layout
$html1 = $this->getView()->render(PATH_TPL.'/layout/'.$this->main_view.'.php', array_merge($paramsExtra,array('content'=>$html1)));
if ( $return ) {
return $html1;
} else {
echo $html1;
exit;
}
}
//这个方法主要用于应对不需要layout的情况
public function renderPartial($phtml,$params=array(),$return=false) {
$paramsExtra = array_merge($params,array('actionName'=>$this->getActionId(),'controllerName'=>$this->getControllerId()));
$paramsExtra['C'] = $this;
$html1 = $this->render($phtml,$paramsExtra);
if ( $return ) {
return $html1;
} else {
echo $html1;
exit;
}
}
}
</pre>
===路由===
*路由注册的顺序很重要, 最后注册的路由协议, 最先尝试路由, 这就有个陷阱. 请注意.
*路由解析仅仅发生一次,某一个路由协议返回成功以后, 就匹配成功
*路由配置好后需要Yaf_Config::addConfig和Yaf_Router::addRoute两种方法来添加路由协议,才会生效
*路由注册示例代码:
<pre>
class Bootstrap extends Yaf_Bootstrap_Abstract{
public function _initConfig(){
Yaf_Registry::set("config", Yaf_Application::app()->getConfig());
}
public function _initRoute() {
$config = Yaf_Registry::get('config');
$routeconfigs = isset($config['routes']) ? $config['routes'] : array();
Yaf_Dispatcher::getInstance()->getRouter()->addConfig($routeconfigs);
}
}
public function rules() {
return array(
array('contact, content', 'required'),
);
}
*controller中可以跟yii一样使用accessRules方法定义未登录和登录用户对action的访问权限
public function accessRules() {
return array(
array('allow',
'actions' => array('*'),
'users' => array('@'),
),
);
}
$redis = new Redis_Connection();
$redis->$key = $value; //配置
$redis->get('key');
//多台服务器
redis4.class="Redis_Connection"
redis4.servers.0.host="10.237.2.72"
redis4.servers.0.port=6379
redis4.servers.1.host="10.237.2.73"
redis4.servers.1.port=6379
//单台服务器
redis7.class="Redis_Connection"
redis7.host="10.237.2.72"
redis7.port=8379
;view中$C是当前contrlloer的实例,对其赋值后可以在页面做相应的展示,如
$C->breadcrumbs = array(
'服务支持' => '/c/service',
'建议留言',
);
$C->pageTitle = "建议留言";
$C->description = "销售渠道。";
$C->keywords = "建议留言";
$C->addCssFile('http://www.baidu.com/css/ui.common.min.css');
$C->addScriptFile('http://www.baidu.com/js/xm.base.min.js');
*PDO的封装,curl的封装等
class Base_Ctrl extends Yaf_Controller_Abstract {
.....
public function getParam($name, $defaultValue=null) {
$value = (null === $defaultValue) ? $this->getRequest()->getParam($name) : $this->getRequest()->getParam($name, $defaultValue);
return !empty($value) ? addslashes($value) : $value;
}
public function getPost($name, $defaultValue=null) {
$value = (null === $defaultValue) ? $this->getRequest()->getPost($name) : $this->getRequest()->getPost($name, $defaultValue);
return !empty($value) ? Common::daddslashes($value) : $value;
}
public function getQuery($name, $defaultValue=null) {
$value = (null === $defaultValue) ? $this->getRequest()->getQuery($name) : $this->getRequest()->getQuery($name, $defaultValue);
return !empty($value) ? Common::daddslashes($value) : $value;
}
.....
}
class ErrorController extends Base_Ctrl {
public function errorAction($exception) {
$exceptionType = get_class($exception);
$this->getView()->assign("error", $exception);
$this->getView()->assign("exceptionType", $exceptionType);
$this->render('error');
}
}