有没有办法从另一个扩展配置条目?

时间:2014-03-15 00:22:28

标签: zend-framework

我已经知道我可以拥有不同的环境,并且可以像[staging:production]那样扩展另一个环境。

我需要的是这样的东西,但是在入门级。

例如,我需要做这样的事情:

actions.game1.action1 = MyAction1
actions.game1.action2 = MyAction2
actions.game1.action3 = MyAction3
actions.game1.action4 = MyAction4

actions.game2 : actions.game1      <<< I need something like this to "extend" game2 from game1
actions.game2.action5 = MyAction5

2 个答案:

答案 0 :(得分:1)

我在第一个附近有另一个解决方案 如果您的操作独立于环境(生产,测试......),您可以浏览另一个配置文件 在configs目录中,创建一个文件games_ini.php,如下所示:

<?php 
////////////////////////////////////////////////////////////////////////
//  GAME1
////////////////////////////////////////////////////////////////////////
$games = array(
    'actions' => array('game1' => array('action1' => 'MyAction1', // actions.game1.action1 = MyAction1
                                        'action2' => 'MyAction2', // actions.game1.action2 = MyAction2
                                        'action3' => 'MyAction3', // actions.game1.action3 = MyAction3
                                        'action4' => 'MyAction4',) // actions.game1.action4 = MyAction4
                      )
);
////////////////////////////////////////////////////////////////////////
//  GAME2
////////////////////////////////////////////////////////////////////////
$games['actions']['game2'] = $appli['actions']['game1']; // actions.game2 : actions.game1      <<< I need something like this to "extend" game2 from game1
$games['actions']['game2']['action5'] = 'MyAction5';     // actions.game2.action5 = MyAction5
////////////////////////////////////////////////////////////////////////


return $games;

无论您需要它(引导程序,控制器,库,...),都可以这样调用:

    $options = new Zend_Config(require APPLICATION_PATH . '/configs/games_ini.php');

要访问您的操作:

    var_dump($options->actions->game1->action1);
    var_dump($options->actions->game2);

否则,本着同样的精神,你可以创造另一个。像这样的Ini文件:

[game1]
actions.game.action1 = MyAction1
actions.game.action2 = MyAction2
actions.game.action3 = MyAction3
actions.game.action4 = MyAction4
[game2]
actions.game.action5 = MyAction5

用Zend_Config_Ini阅读。 我没有测试,但它也应该工作:)

我希望它能回答你的问题:)

答案 1 :(得分:0)

我建议你通过php文件浏览Zend_Config。

1 - 通过application_ini.php替换application.ini
例如:标准application.ini

[production]
phpSettings.display_startup_errors = 0
phpSettings.display_errors = 0
phpSettings.date.timezone = "Europe/Paris"
includePaths.library = APPLICATION_PATH "/../library"
bootstrap.path = APPLICATION_PATH "/Bootstrap.php"
bootstrap.class = "Bootstrap"
appnamespace = "Application"
resources.frontController.controllerDirectory = APPLICATION_PATH "/controllers"
resources.frontController.params.displayExceptions = 0
resources.db.adapter = PDO_MYSQL
resources.db.params.host = localhost
resources.db.params.username = toto
resources.db.params.password = "tata"
resources.db.params.dbname = dbname
resources.layout.layoutPath = APPLICATION_PATH "/layouts/scripts/"
resources.view.doctype = "XHTML1_STRICT"
resources.view.charset = "UTF-8"
;; Log
resources.log.stream.writerName = "Stream"
resources.log.stream.writerParams.stream = APPLICATION_PATH "/../logs/application.log"
resources.log.stream.writerParams.mode = "a"
[staging : production]

[testing : production]
phpSettings.display_startup_errors = 1
phpSettings.display_errors = 1

[development : production]
phpSettings.display_startup_errors = 1
phpSettings.display_errors = 1
resources.frontController.params.displayExceptions = 1

由此application_ini.php

<?php 
/*
*/
////////////////////////////////////////////////////////////////////////
// PRODUCTION
////////////////////////////////////////////////////////////////////////
$appli = array(// Config du php.ini 
    'phpSettings' => array(    
         'display_startup_errors' => 0,        //phpSettings.display_startup_errors = 0
         'display_errors'         => 0,        //phpSettings.display_errors = 0
         'date'                   => array('timezone' => 'Europe/Paris',), //phpSettings.date.timezone = "Europe/Paris"
         ),
    // LibrarY           
     'includePaths' => array(
           'library' => APPLICATION_PATH . '/../library' // includePaths.library = APPLICATION_PATH "/../library"
           ),
      // Bootstrap
      'bootstrap' => array(
           'path'   => APPLICATION_PATH . '/Bootstrap.php', // bootstrap.path = APPLICATION_PATH "/Bootstrap.php"
           'class'  => 'Bootstrap', // bootstrap.class = "Bootstrap"
                 ),
      // Nom de l'application
      'appnamespace' => 'Application', // appnamespace = "Application"
      // Resources
      'resources' => array( 
        // Controller
        'frontController' => array(
                'controllerDirectory' => APPLICATION_PATH . '/controllers', // resources.frontController.controllerDirectory = APPLICATION_PATH "/controllers"
                'params'              => array('displayExceptions' => 0), // resources.frontController.params.displayExceptions = 0
                ),
         // db
         'db' => array('adapter' => 'PDO_MYSQL', // resources.db.adapter = PDO_MYSQL
                       'params' => array('host'     => 'localhost', // resources.db.params.host = localhost
                                         'username' => 'toto', // resources.db.params.username = toto
                                         'password' => 'tata', // resources.db.params.password = "tata"
                                         'dbname'   => 'dbname' // resources.db.params.dbname = dbname
                                         ) 
         ),        
         // Layout  
         'layout' => array(
                'layoutPath' => APPLICATION_PATH . '/layouts/scripts/' // resources.layout.layoutPath = APPLICATION_PATH "/layouts/scripts/"
                ),

        'view' => array(        
                   'doctype' => 'XHTML1_STRICT', // resources.view.doctype = "XHTML1_STRICT"
                   'charset' => 'UTF-8', // resources.view.charset = "UTF-8"
                        ),
         // Logs    
         'log' => array(
                    'stream' => array(
                          'writerName'   => 'Stream', // resources.log.stream.writerName = "Stream"
                          'writerParams' => array('stream'   => APPLICATION_PATH . '/../logs/application.log', // resources.log.stream.writerParams.stream = APPLICATION_PATH "/../logs/application.log"
                                                  'mode'     => 'a', // resources.log.stream.writerParams.mode = "a"                                                                                                                 
                                                             ),                                                                 
                                        ),                          
                        ),
        ),  // End Resources
    'actions' => array('game1' => array('action1' => 'MyAction1', // actions.game1.action1 = MyAction1
                                        'action2' => 'MyAction2', // actions.game1.action2 = MyAction2
                                        'action3' => 'MyAction3', // actions.game1.action3 = MyAction3
                                        'action4' => 'MyAction4',) // actions.game1.action4 = MyAction4
                      )
);
$appli['actions']['game2'] = $appli['actions']['game1']; // actions.game2 : actions.game1      <<< I need something like this to "extend" game2 from game1
$appli['actions']['game2']['action5'] = 'MyAction5';     // actions.game2.action5 = MyAction5
////////////////////////////////////////////////////////////////////////
//  STAGING
////////////////////////////////////////////////////////////////////////

////////////////////////////////////////////////////////////////////////
//  TEST    
////////////////////////////////////////////////////////////////////////
// Modification pour l'environnement test
if (APPLICATION_ENV == 'testing') { 
    $appli['phpSettings']['display_startup_errors'] = 1; // phpSettings.display_startup_errors = 1
    $appli['phpSettings']['display_errors'] = 1; // phpSettings.display_errors = 1
}               
////////////////////////////////////////////////////////////////////////
//  DEVELOPPEMENT   
////////////////////////////////////////////////////////////////////////
// Modification pour l'environnement de dev
if (APPLICATION_ENV == 'development') {
    $appli['phpSettings']['display_startup_errors'] = 1; // phpSettings.display_startup_errors = 1
    $appli['phpSettings']['display_errors']         = 1; // phpSettings.display_errors = 1
    // Controller
    $appli['resources']['frontController']['params']['displayExceptions'] = 1; // resources.frontController.params.displayExceptions = 1
}

return $appli;

最后,您会看到我添加了您的示例Game1和game2

2 - 在index.php中,替换

$application = new Zend_Application(
    APPLICATION_ENV,
    APPLICATION_PATH . '/configs/application.ini'
);

通过

$application_ini = require APPLICATION_PATH . '/configs/application_ini.php';

// Create application, bootstrap, and run
$application = new Zend_Application(APPLICATION_ENV,
                                    $application_ini
                                    );

3-完成后,添加你的引导程序:

protected function _initActions() {

    $options = $this->getOptions();
    if (isset($options['actions'])){
         $actions_options = $options['actions'];
         var_dump($actions_options['game1']); 
         var_dump($actions_options['game2']);
    }
}

结果如下:

array
  'action1' => string 'MyAction1' (length=9)
  'action2' => string 'MyAction2' (length=9)
  'action3' => string 'MyAction3' (length=9)
  'action4' => string 'MyAction4' (length=9)

array
  'action1' => string 'MyAction1' (length=9)
  'action2' => string 'MyAction2' (length=9)
  'action3' => string 'MyAction3' (length=9)
  'action4' => string 'MyAction4' (length=9)
  'action5' => string 'MyAction5' (length=9)

答案有点长,但我希望用php文件显示Zend_Config的功能,这些文件比ini文件更有效 :)