PHPEye开源社区 » Zend Framework 使用讨论 » 每次都load ini,很耗時,很不爽,怎麼讓它只load一次ini?
《Programming PHP》第二版上市
2008-7-15 08:31 sentrychen
呵呵,我也准备去掉smarty,因为有了layout和form之后,实在没有smarty太多活了

2008-7-16 11:11 sentrychen
对于ajax提交,我不喜欢初始化smarty,layout,不需要viewRenderer,errorHandle,jasonqi帮忙看看这样改行不行

[php]
<?php

if (!defined('ROOT_DIR')) {
    define('ROOT_DIR', dirname(dirname(dirname(__FILE__))));
}

//bootstrap class
class Itc_App
{
    protected static $_instance = null;
    protected $_services = Array();
    protected $_disables = Array();
   
    protected function __construct ()
    {
        set_include_path('.'
            . PATH_SEPARATOR . ROOT_DIR . '/lib/'
            . PATH_SEPARATOR . ROOT_DIR . '/app/default/models/'
            . PATH_SEPARATOR . get_include_path()
        );
        require_once 'Zend/Loader.php';
        Zend_Loader::registerAutoload();
        
        $config = new Zend_Config_Ini(ROOT_DIR . '/etc/config.ini', 'general');
        Zend_Registry::set('config', $config);
        $this->_services['config'] = $config;
        
        //if ajax submit
        if (isset($_SERVER['HTTP_X_REQUESTED_WITH']) && $_SERVER['HTTP_X_REQUESTED_WITH'] == 'XMLHttpRequest'){
            $this->_disables = array('smarty','layout','viewRenderer','errorHandler','throwExceptions');
        }
    }
   
    public static function getInstance()
    {
        if (null === self::$_instance) {
            self::$_instance = new self();
        }
        return self::$_instance;
    }
   
    public function __get($name)
    {
        if (!array_key_exists($name, $this->_services)) {
            return false;
        }
        return $this->_services[$name];
    }
   
    public function initSession()
    {
        if ($this->session){
            return $this;
        }
        Zend_Session::setOptions($this->config->session->toArray());
        $this->_services['session'] = true;
        return $this;
    }
   
    public function initTimezone()
    {
        if ($this->timezone){
            return $this;
        }
        $timezone = $this->config->sys->timezone;
        date_default_timezone_set($this->config->sys->timezone);
        $this->_services['timezone'] = $timezone;
        return $this;
    }
   
    public function initTranslate()
    {
        if ($this->translate){
            return $this;
        }
        $translate = Itc_Translate::startTranslate($this->config->lang->toArray());            
        Zend_Registry::set('Zend_Translate', $translate);
        $this->_services['translate'] = $translate;
        return $this;
    }
   
    public function initCache()
    {
        if ($this->cache){
            return $this;
        }
        $cache = Zend_Cache::factory('Core', 'File',
                  $this->config->cache->frontend_options->toArray(),
                  $this->config->cache->backend_options->toArray());
        $cache->setOption('caching', ! $this->config->sys->debugging);
        $this->_services['cache'] = $cache;
        return $this;
    }
   
    public function initDb()
    {
        if ($this->db){
            return $this;
        }
        $db = Zend_Db::factory($this->config->db);
        $db->query("set names {$this->config->sys->charset};");
        Zend_Registry::set('db', $db);
        Itc_Db_Table::setDefaultAdapter($db);
        if ($this->cache){
            Itc_Db_Table::setDefaultMetadataCache($this->cache);
        }
        $this->_services['db'] = $db;
        return $this;
    }
   
    public function initSmarty()
    {
        if ($this->smarty){
            return $this;
        }
        $smarty = new Itc_View_Smarty($this->config->smarty->toArray());

        $viewRenderer = Zend_Controller_Action_HelperBroker::getStaticHelper('viewRenderer');
        $viewRenderer->setView($smarty)->setViewSuffix($this->config->smarty->suffix);
        $this->_services['smarty'] = $smarty;
        return $this;
    }
   
    public function initLayout()
    {
        if ($this->layout){
            return $this;
        }
        
        $options = array();
        $options['layoutPath'] =$this->config->layout->layout_path;
        
        if ($this->smarty){
            $options['view'] = $this->smarty;
        }
        
        $layout = Zend_Layout::startMvc($options);
        $this->_services['layout'] = $layout;
        return $this;
    }
   
    public function disable($disables = null)
    {
        $disables = (array) $disables;
        foreach ($disables as $disable){
            $this->_disables[$disable] = true;        
        }
        return $this;
    }
   
    public function isDisabled($name)
    {
        return isset($this->_disables[$name]);
    }
   
    public function run()
    {
        
        if (!$this->isDisabled('session')){
            $this->initSession();
        }
        
        if (!$this->isDisabled('timezone')){
            $this->initTimezone();
        }
        
        if (!$this->isDisabled('translate')){
            $this->initTranslate();
        }
        
        if (!$this->isDisabled('cache')){
            $this->initCache();
        }
        
        if (!$this->isDisabled('db')){
            $this->initDb();
        }
        
        if (!$this->isDisabled('smarty')){
            $this->initSmarty();
        }
   
        if (!$this->isDisabled('layout')){
            $this->initLayout();
        }
        
        if (!$this->isDisabled('dispatch')){
            $frontController = Zend_Controller_Front::getInstance();
            $frontController->setControllerDirectory(array('default' => ROOT_DIR . '/app/default/controllers'));
            $frontController->setBaseUrl($this->config->www->baseUrl);
            if ($this->isDisabled('viewRenderer')){
                $frontController->setParam('noViewRendere',true);
            }
            if ($this->isDisabled('errorHandler')){
                $frontController->setParam('noErrorHandler',true);
            }
            
            if ($this->isDisabled('throwExceptions'))
            {
                $frontController->dispatch();
            }
            else
            {
                $frontController->throwExceptions(true);
                                                
                try{
                   $frontController->dispatch();
                }catch (Zend_Controller_Exception $e) {
                        include ROOT_DIR . '/pub/errors/404.phtml';
                } catch (Exception $e) {
                        include ROOT_DIR . '/pub/errors/500.phtml';
               
                }
            }
        }

    }
}
[/php]

2008-7-16 11:32 sentrychen
再问jasonqi一个问题,如果我的页面结构中除了content的内容会变化之外(常见后台管理系统),其它的都不会变化,是不是使用iframe比layout更好?

2008-7-16 11:58 sentrychen
改变主意了。。。天哪,为什么我要改来改去的?我决定全部使用ajax。。。no layout,no iframe

2008-7-16 13:53 jasonqi
说实话,你的bootstrap文件比我的要好,如果非要挑个骨头,那么

if (!defined('ROOT_DIR')) {
    define('ROOT_DIR', dirname(dirname(dirname(__FILE__))));
}

不如, 把ROOT_DIR做成静态变量,这样会节省执行时间,不必在每个地方都执行dirname ...

2008-7-16 13:56 jasonqi
[quote]原帖由 [i]sentrychen[/i] 于 2008-7-16 11:32 发表 [url=http://www.phpeye.com/bbs/redirect.php?goto=findpost&pid=2282&ptid=612][img]http://www.phpeye.com/bbs/images/common/back.gif[/img][/url]
再问jasonqi一个问题,如果我的页面结构中除了content的内容会变化之外(常见后台管理系统),其它的都不会变化,是不是使用iframe比layout更好? [/quote]

好像Google ads 不喜欢 iframe  (跑题了,其实是我不懂iframe)

2008-7-16 15:16 sentrychen
[quote]原帖由 [i]jasonqi[/i] 于 2008-7-16 13:53 发表 [url=http://www.phpeye.com/bbs/redirect.php?goto=findpost&pid=2286&ptid=612][img]http://www.phpeye.com/bbs/images/common/back.gif[/img][/url]
说实话,你的bootstrap文件比我的要好,如果非要挑个骨头,那么

if (!defined('ROOT_DIR')) {
    define('ROOT_DIR', dirname(dirname(dirname(__FILE__))));
}

不如, 把ROOT_DIR做成静态变量,这样会节省执行时间,不必在每 ... [/quote]
呵呵,其实我这个bootstrap是受了你前面的回复的启发才写的。还得谢谢你呢。
ROOT_DIR这样写确实有点浪费时间,我原来的打算是对于不同的项目尽量不去修改bootstrap文件,只需要修改config.ini就可以了。
不过考虑到性能至上,还是采纳你的意见吧。bootstrap的维护量确实是很少的。

2008-7-16 15:18 sentrychen
[quote]原帖由 [i]jasonqi[/i] 于 2008-7-16 13:56 发表 [url=http://www.phpeye.com/bbs/redirect.php?goto=findpost&pid=2287&ptid=612][img]http://www.phpeye.com/bbs/images/common/back.gif[/img][/url]


好像Google ads 不喜欢 iframe  (跑题了,其实是我不懂iframe) [/quote]

我现在全部使用ajax来做,将每次ajax返回的结果替换原来layout中的content就行了。

2008-7-30 14:47 ab__
sentrychen 的BOOTSTAR文件 是通过 $this->_disables 这一数组实现具体在启动ZF时加载那些内容 比如说 链接数据库 如果我的某一个控制器内 的动作不是全部都需要链接数据库 那么我应该如何动态在这些动作内 设置我们某一个动作具体是否要加载数据库类进来呢

2008-8-2 00:01 sentrychen
方法1,写一个action助手插件,在bootstrap里面配置哪个controller哪个action需要加载数据库
配置方法类似$options=array(
  '^/controllerName' => true;
  '^/controllerName/actionName' => false;
);
方法2,在bootstrap里面设置不启动数据库,在具体的controller里面再使用Itc_App::getInstance->initDb();

方法3,自己扩展Zend_Db类,自己写一个静态的工厂方法,另外加一个静态的参数设置方法。这样你在每次调用这个工厂的时候,就会自动判断是否已经存在了一个静态的数据库对象,如果存在就返回,如果不存在就根据静态的参数创建之。如果还没有设置静态参数或者在你调用这个工厂的时候没有传入,报错吧。。。不过这个问题一般不会发生,因为你肯定要在bootstrap里面设置好DSN参数。
另外保存数据库对象的静态属性最好是一个数组。。。。

[[i] 本帖最后由 sentrychen 于 2008-8-2 00:14 编辑 [/i]]

2008-8-4 08:56 ab__
说的比较详细了非常感谢

2008-8-4 14:35 ab__
另外还想问下sentrychen
你启动文件里 Itc_开头的一些内容都是自己定义的一些扩展类么?
另外路径是不是要单独 卸载 library 里
比如 结构想这样
library/
        /zend
        /Itc
然后在Itc里又定义了一些类 比如 DB

2008-8-4 14:58 sentrychen
是的

2008-8-4 15:20 ab__
多谢 :lol

页: 1 [2]


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