全部博文(921)
分类:
2008-12-23 23:27:40
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扩展总结一下