2008-1-2 17:33
mmocom
我在1.03版本里运行的index.php
[php]<?php
error_reporting(E_ALL|E_STRICT);
date_default_timezone_set('Asia/Shanghai');
$ds = DIRECTORY_SEPARATOR;//目录分隔符号
$ps = PATH_SEPARATOR;//路径分隔符号
define('ABS_ROOT_PATH', realpath(dirname(__FILE__)));
define('REL_ROOT_PATH', substr($_SERVER['PHP_SELF'], 0 , strpos($_SERVER['PHP_SELF'],'/index.php')));
set_include_path('.'.PATH_SEPARATOR.'./library'.PATH_SEPARATOR.'./application/models/'.PATH_SEPARATOR.get_include_path());
require_once 'Zend/Loader.php';
//自动加载类
Zend_Loader::registerAutoload();
//数据库配置
$dbParams = new Zend_Config_Ini('./config/config.ini','SysDB');
$db = Zend_Db::factory($dbParams->adapter,$dbParams->toArray());
$db->query("set names {$dbParams->charset};");
Zend_Db_Table::setDefaultAdapter($db);
Zend_Registry::set('sysdb',$db);
//SMARTY配置
$smartyParams = new Zend_Config_Ini('./config/config.ini', 'SysTpl');
$smarty = new Custom_View_Smarty($smartyParams->toArray());
Zend_Registry::set('systpl',$smarty);
//全局定义
$sysinfoParams = new Zend_Config_Ini('./config/config.ini','SysGlobalInfo');
Zend_Registry::set('sysinfo',$sysinfoParams->toArray());
//初始控制器
$router = new Zend_Controller_Router_Rewrite();
$frontController = Zend_Controller_Front::getInstance();
$frontController->setRouter($router);
$frontController->setControllerDirectory(array(
'default' => './application/controllers',
));
$frontController->throwExceptions(true);
$frontController->setParam('noViewRenderer',true);
$frontController->setBaseUrl(REL_ROOT_PATH);
try{
$frontController->dispatch();
}catch(Zend_Controller_Dispatcher_Exception $e){
echo nl2br($e->__toString());
}
unset($db);
unset($smarty);
?>
[/php]
config.ini
[code][SysDB]
adapter = pdo_mysql
host = localhost
username = root
password =
dbname = mmocom
charset = utf8
prefix =
[SysGlobalInfo]
test = true
[SysTpl]
left_delimiter = "{"
right_delimiter = "}"
template_dir = "./www/templates"
compile_dir = "./www/templates_c"[/code]
IndexController.php
[php]<?php
class IndexController extends Zend_Controller_Action
{
public function preDispatch()
{
}
public function init()
{
$this->db = Zend_Registry::get('sysdb');
$this->tpl = Zend_Registry::get('systpl');
$this->globalinfo = Zend_Registry::get('sysinfo');
}
public function indexAction()
{
$this->tpl->display('index.html');
}
public function testAction()
{
echo "test";
echo __FILE__;
}
public function __call($method, $args)
{
if ('Action' == substr($method, -6))
{
// return $this->_forward('index');
echo 'Invalid method "' . $method . '" called';
exit;
}
throw new Exception('Invalid method "' . $method . '" called');
}
public function noRouterAction()
{
// $this->_redirect('/');
}
}
?>[/php]
大家的意见是我努力的动力:)
[[i] 本帖最后由 mmocom 于 2008-1-2 17:45 编辑 [/i]]
2008-1-5 15:07
cosnis
? models ? module?
我觉得ZF的 autoload 不是很爽
要
autoload( 'class' );
autoload( 'class' );
autoload( 'class' );
autoload( 'class' );
的用....
我的autoload
function __autoload( $class )
{
if( is_array( $class ) ) { foreach( $class as $val ) { Zend_Loader::loadClass( $val ); } }
else { Zend_Loader::loadClass( $class ); }
}
另外 为什么 我的 public function noRouterAction()
不起作用 请求一个空的 controller 的时候是报错.....
不喜欢 echo nl2br($e->__toString());
用 zend_debug::dump(); 嘿嘿
[[i] 本帖最后由 cosnis 于 2008-1-5 15:37 编辑 [/i]]