PHPEye开源社区 » Zend Framework 使用讨论 » 我在1.03版本里运行的index.php
《Programming PHP》第二版上市
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-2 22:30 haohappy
不错,对新手很有用,加入精华:D

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]]

2008-1-9 22:41 断桥残雪
如何设置数据表前缀呢?

2008-1-10 18:45 huangam
回复 #3 cosnis 的帖子

/////另外  为什么 我的 public function noRouterAction()

我是这个也不起作用

2008-1-14 22:01 Cykit
似乎是 noRouteAction , Route 不是 Router

2008-1-24 19:26 frogsky
:loveliness: 对新手有帮助o(∩_∩)o...。。。

2008-2-23 17:50 yeahdwb
找了两天了,终于看到了想看到的东西了 谢谢楼主

页: [1]


Powered by Discuz! Archiver 5.5.0  © 2001-2006 Comsenz Inc.