Chinaunix首页 | 论坛 | 博客
  • 博客访问: 5017741
  • 博文数量: 921
  • 博客积分: 16037
  • 博客等级: 上将
  • 技术积分: 8469
  • 用 户 组: 普通用户
  • 注册时间: 2006-04-05 02:08
文章分类

全部博文(921)

文章存档

2020年(1)

2019年(3)

2018年(3)

2017年(6)

2016年(47)

2015年(72)

2014年(25)

2013年(72)

2012年(125)

2011年(182)

2010年(42)

2009年(14)

2008年(85)

2007年(89)

2006年(155)

分类:

2008-12-23 23:27:40

这些日子给忙死了,忙着许多事情。难得清闲,今天就将zf框架的入口文件和部分配置总结一下。因为个人使用习惯,配置有点个人色彩,大家觉得有哪里需要改进,欢迎留言。

     为方便起见,配置没有采用多模块设计,只是使用了默认的模块(default)。文件结构布局如下所示:
     
app/
controllers/
IndexController.php
cache/ //smarty缓存目录(后续我会介绍将smarty整合进zf框架)
compile/ //smarty编译后文件存放目录
views/
scripts/
index/
index.phtml
lib/
Zend/
DB/
smarty/
application.php
config.ini

html/
cache/ //zf缓存目录
css/
js/
images/
.htaccess
index.php
目录如上所示,其中的扩展我会在后续总结。

入口文件:index.php
ini_set('include_path','../lib'); //如果有多个路径,注意在Linux系统下用 : (分号)分割
ini_set('display_errors','on'); //on显示错误 off 不显示错误
include 'Zend/Loader.php';
include 'application.php'; //许多初始化的文件都在这里
Zend_Session::start();
function __autoload($class) //自动加载类
{
if (class_exists($class))
return ;
$path = str_replace('_', DIRECTORY_SEPARATOR, $class);
if(! @require_once $path . '.php'){
Zend_Loader::loadClass($class);
}
}
$app = new application();
$app->init();


application.php
class application
{
private $_controller = null;
private $_config = null;
private $_pathConfig = null;

/**
* 构造函数
*/
public function __construct()
{
Zend_Loader::loadClass('Zend_Controller_Front');
Zend_Loader::loadClass('Zend_Config_Ini');
Zend_Loader::loadClass('Zend_Registry');
Zend_Loader::loadClass("Zend_View");
Zend_Loader::loadClass('Zend_Db');
}

/**
* 初始化模块
*/
public function init()
{
$this->_initConfig();
$this->_initController();
$this->_initView();
$this->_initDb();
$this->_controller->returnResponse(true);
$response = $this->_controller->dispatch();
if ($response->isException()) {
$exceptions = $response->getException();
}
else {
$response->sendHeaders();
$response->outputBody();
}

}

/**
* 初始化配置信息
*/
private function _initConfig()
{
$this->_config = new Zend_Config_Ini('config.ini','mysqli');
$this->_pathConfig = new Zend_Config_Ini('config.ini','path');
}

/**
* 初始化控制器
*/
private function _initController()
{
$this->_controller = Zend_Controller_Front::getInstance();
/**
* 自定义参数,是否检测控制器是否存在,true 为controller不存在时跳转到控制器不存在提示页面
*/
$this->_controller->setParam("detectControllerExists",true) ;

/**
* 打开异常处理,若此项为false的话,将不抛出异常
* 注意: 网站投入使用后关闭此项
*/
$this->_controller->throwExceptions(true);

$this->_controller->setControllerDirectory($this->_pathConfig->dir->controller);

$registry = Zend_Registry::getInstance();
$registry->set('app',$this->_controller);
}
/**
* 初始化视图
*/
private function _initView()
{
$view = new Zend_View();
$view->setBasePath($this->_pathConfig->dir->viewBase);

/**
* 添加htm文件的存放目录
*/
$view->addScriptPath($this->_pathConfig->dir->viewBase."/htm");
/**
* 添加php脚本的存放目录
*/
$view->addScriptPath($this->_pathConfig->dir->viewBase."/scripts");

$registry = Zend_Registry::getInstance();
$registry->set('view',$view);
}

/**
* 初始化数据库连接
*
*/
private function _initDb()
{
$DB = Zend_Db::factory($this->_config->db->adapter,$this->_config->db->config->toArray());
$DB->query("set names ".$this->_config->db->config->charset);
Zend_Registry::set("DB",$DB);
}
}


配置文件:config.ini

[mysqli]
db.adapter = Mysqli
db.config.host = IP
db.config.username = username
db.config.password = password
db.config.dbname = dbname
db.config.port = 3306
db.config.charset = gbk
[path]
dir.controller = ../app/controllers
dir.viewBase = ../app/views
dir.compile = ../app/compile
dir.cache = ../app/cache


配置部分我就介绍到这了,大家有什么意见,不要吝啬哦!

后面我会将smarty扩展总结一下
阅读(2179) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~