PHPEye开源社区 » Zend Framework 使用讨论 » 关于引导文件 请大家指教【解决】
《Programming PHP》第二版上市
2008-6-13 10:13 dudu
关于引导文件 请大家指教【解决】

把这种结构
[attach]74[/attach]
改成这种结构
[attach]73[/attach]
引导文件要改什么地方
这是引导文件
[php]

final class BlogSite
{
    private $frontCtroller = null; //前端控制器变量

    private $config = null;

    private $smarty = null;

    private $dbAdapter = null;

    private static $instance = null;

    public static function getInstance()
    {
            
                self::$instance = new BlogSite();
            self::$instance->init();
            self::$instance->buildFrontCtrl();               
            self::$instance->buildDbAdapter();  

            return  self::$instance;
    }

    public function __construct()
    {

    }

    private function init()
    {
        Zend_Loader::registerAutoload();  
        $this->buildConfig();
    }

    private function buildConfig()  // 加载config文件
    {
        $this->config = new Zend_Config_Ini(CONFIG_FILE,'config');

        Zend_Registry::set('config', $this->config);     
        return $this->config;
    }

    private function buildFrontCtrl() // 加载前端控制器
    {
        $this->frontCtroller = Zend_Controller_Front::getInstance();
        $modules = $this->config->module->toArray();
      
        foreach ($modules as $key=>$value){
                $module[$key] = APP_DIR . DIRECTORY_SEPARATOR.'modules' . DIRECTORY_SEPARATOR . $key . DIRECTORY_SEPARATOR . $value;
        }
        
        $this->frontCtroller->setControllerDirectory($module);
      

        $this->frontCtroller->setParam('noViewRenderer', true);
        $this->frontCtroller->throwExceptions(true);
        
        return $this->frontCtroller;
    }
   
    private function buildDbAdapter()//加载数据库设置
    {
            $this->dbAdapter = Zend_Db::factory($this->config->db->adapter,$this->config->db->config->toArray());
            
            $this->dbAdapter->query('set names ' . $this->config->db->config->charset);
        
        Zend_Registry::set('dbAdapter', $this->dbAdapter);  //注册变量      
        return $this->dbAdapter;
    }

    public function run()
    {
        $this->frontCtroller->dispatch();
    }
}

$blogsite = BlogSite::getInstance();
$blogsite->run();
[/php]
修改后的目录结构
[attach]75[/attach]
修改后的引导文件
[php]
<?php

final class BlogSite
{
    private $frontCtroller = null; //前端控制器变量

    private $config = null;

    private $smarty = null;

    private $dbAdapter = null;

    private static $instance = null;

    public static function getInstance()
    {
            
                self::$instance = new BlogSite();
            self::$instance->init();
            self::$instance->buildFrontCtrl();               
            self::$instance->buildDbAdapter();  

            return  self::$instance;
    }

    public function __construct()
    {

    }

    private function init()
    {
        Zend_Loader::registerAutoload();  
        $this->buildConfig();
    }

    private function buildConfig()  // 加载config文件
    {
        $this->config = new Zend_Config_Ini(CONFIG_FILE,'config');

        Zend_Registry::set('config', $this->config);     
        return $this->config;
    }

    private function buildFrontCtrl() // 加载前端控制器
    {
        $this->frontCtroller = Zend_Controller_Front::getInstance();
       // $modules = $this->config->module->toArray();
      
       // foreach ($modules as $key=>$value){
        //        $module[$key] = APP_DIR . DIRECTORY_SEPARATOR.'modules' . DIRECTORY_SEPARATOR . $key . DIRECTORY_SEPARATOR . $value;
       // }
        
        //$this->frontCtroller->setControllerDirectory($module);
       $this->frontCtroller->setModuleControllerDirectoryName('controllers');
       $this->frontCtroller->addModuleDirectory(MODULES_DIR);

        $this->frontCtroller->setParam('noViewRenderer', true);
        $this->frontCtroller->throwExceptions(true);
        
        return $this->frontCtroller;
    }
   
    private function buildDbAdapter()//加载数据库设置
    {
            $this->dbAdapter = Zend_Db::factory($this->config->db->adapter,$this->config->db->config->toArray());
            
            $this->dbAdapter->query('set names ' . $this->config->db->config->charset);
        
        Zend_Registry::set('dbAdapter', $this->dbAdapter);  //注册变量      
        return $this->dbAdapter;
    }

    public function run()
    {
        $this->frontCtroller->dispatch();
    }
}

$blogsite = BlogSite::getInstance();
$blogsite->run();


[/php]
还请大家指正
谢谢
                                                 ---dudu

[[i] 本帖最后由 dudu 于 2008-6-13 11:04 编辑 [/i]]

页: [1]


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