设置好zend framework的运行环境后,就可以开始着手程序了。
最简单的方法当然是使用zend studio中的创建zend framework项目了,创建的时候选上default project,那样就可以直接生成一个可运行的实例了。
1,/public/index.php
此为网站入口,通常可以加上以下设置(不作太多解释)
error_reporting(E_ALL|E_STRICT);
date_default_timezone_set('Asia/Shanghai');
define('APPLICATION_ENV','development');
set_include_path('.'.PATH_SEPARATOR.'d:\resources\ZendFramework-1\library');
2,/application/Bootstrap.php
此为framework的引导文件,一般会加上系统初始化的信息,例如数据库,日志之类的,当然数据库需要事前配置好,这里使用了多数据库
protected function _initAutoload ()
{
}
protected function _initDB ()
{
//载入resources
$resource = $this->getPluginResource('multidb');
$resource->init();
//参照: Zend_Application_Resource_Multidb
$db = $resource->getDefaultDb();
//设置dbtable使用的默认数据库
Zend_Db_Table::setDefaultAdapter($db);
//加载另外一个库并存入公共寄存器
$db2 = $resource->getDb('db2');
Zend_Registry::set("mydb2", $db2);
}
protected function _initView ()
{}
protected function _initLogger ()
{
$logger = new Zend_Log();
$writer = new Zend_Log_Writer_Stream('../logs/err.log', 'a+');
$logger->addWriter($writer);
Zend_Registry::set('mylog', $logger);
return $logger;
}
3,/application/configs/application.ini
此为系统的设置文件,一些基本的设置信息都在这里
这里是多数库的相应设置,可以是一个MYSQL,另一个SQLITE
[production]
resources.multidb.db1.adapter=PDO_MYSQL
resources.multidb.db1.driver_options.1002="SET NAMES UTF8 ;"
resources.multidb.db1.host="localhost"
resources.multidb.db1.username="root"
resources.multidb.db1.password=""
resources.multidb.db1.dbname="db1"
resources.multidb.db1.default=true
resources.multidb.db2.adapter=PDO_MYSQL
resources.multidb.db2.driver_options.1002="SET NAMES UTF8 ;"
resources.multidb.db2.host="localhost"
resources.multidb.db2.username="root"
resources.multidb.db2.password=""
resources.multidb.db2.dbname="db2"
4,/application/layouts/layout.phtml
此为布局文件,默认的实例中不包含此文件需自行生成,使用以下命令
zf enable layout
zf tool有相当强大的功能,但暂未没找到详尽的命令行说明
前端的显示一般分为,头,脚,内容,几部份,详细划分按各自喜好则可。
值得一提的是模版phtml中的变量引用会从原来的view类中
$this->AAA
改为layout类的
$this->layout()->AAA