2009-11-05 14 views
22

私はquick-startセットアップに基づいてZend Frameworkアプリケーションを持っています。コントローラからZend Frameworkアプリケーションの設定にアクセスするにはどうすればよいですか?

デモが動作していて、実際の作業を行うために新しいモデルクラスをインスタンス化する時点です。私のコントローラでは、私は、私のモデルのコンストラクタに、このようなものを(のapplication.iniで指定)設定パラメータを渡したい:

class My_UserController extends Zend_Controller_Action 
{ 
    public function indexAction() 
    { 
     $options = $this->getFrontController()->getParam('bootstrap')->getApplication()->getOptions(); 
     $manager = new My_Model_Manager($options['my']); 
     $this->view->items = $manager->getItems(); 
    } 
} 

の例では、上記のオプションへのアクセスを許可していますが、非常にラウンドについてのようです。設定にアクセスするより良い方法はありますか?

答えて

47

私はいつもレジストリに設定を渡すために私のブートストラップに次のinit-methodを追加します。

protected function _initConfig() 
{ 
    $config = new Zend_Config($this->getOptions(), true); 
    Zend_Registry::set('config', $config); 
    return $config; 
} 

これはあなたのコードを少し短くなります。

また
class My_UserController extends Zend_Controller_Action 
{ 
    public function indexAction() 
    { 
     $manager = new My_Model_Manager(Zend_Registry::get('config')->my); 
     $this->view->items = $manager->getItems(); 
    } 
} 
+0

これは、アレイをオブジェクトに再解析するために少し時間がかかります。あなたが配列としての設定を好むなら、それは単なる "Zend_Registry :: set( 'config'、$ this-> getOptions());"値を取得する前に、変数に値を取得する必要があります。 –

+0

@Alister:そうです。オプション配列をレジストリの中に格納するほうが速いのですが、単一の値を取得するたびに配列を格納するのは面倒です。 –

+2

これは、以下の$ GLOBALS ['application']のアイデアと変わらないもので、$ GLOBALS ['application']がおそらく99%働いているという利点があります。 –

6

、代わりに特別な場合を除き、Zend_Registryを使用してのあなたにもできるように、パブリックメンバ関数で、すべてのアプリケーションの情報が含まれていますシングルトンApplicationクラスを作成することができます関連するデータにアクセスする必要があります。あなたは、関連するコードとスニペットを見ることができます(それはあなたにそれがどのように実装できるかのアイデアを与えることを、そのまま実行されません):

final class Application 
{ 
    /** 
    * @var Zend_Config 
    */  
    private $config = null; 

    /** 
    * @var Application 
    */  
    private static $application; 

    // snip 

    /** 
    * @return Zend_Config 
    */ 
    public function getConfig() 
    { 
     if (!$this->config instanceof Zend_Config) { 
      $this->initConfig(); 
     } 
     return $this->config; 
    } 

    /** 
    * @return Application 
    */ 
    public static function getInstance() 
    { 
     if (self::$application === null) { 
      self::$application = new Application(); 
     } 
     return self::$application; 
    } 

    /** 
    * Load Configuration 
    */ 
    private function initConfig() 
    { 
     $configFile = $this->appDir . '/config/application.xml'; 
     if (!is_readable($configFile)) { 
      throw new Application_Exception('Config file "' . $configFile . '" is not readable'); 
     } 
     $config = new Zend_Config_Xml($configFile, 'test'); 
     $this->config = $config; 
    } 

    // snip 

    /** 
    * @param string $appDir 
    */ 
    public function init($appDir) 
    { 
     $this->appDir = $appDir; 
     $this->initConfig(); 
     // snip 
    } 

    public function run ($appDir) 
    { 
     $this->init($appDir); 
     $front = $this->initController(); 
     $front->dispatch();    
    } 
} 

あなたのブートストラップは、次のようになります。

require 'Application.php'; 
try { 
    Application::getInstance()->run(dirname(dirname(__FILE__))); 
} catch (Exception $e) { 
    header("HTTP/1.x 500 Internal Server Error"); 
    trigger_error('Application Error : '.$e->getMessage(), E_USER_ERROR); 
} 

あなたは以下を使用する設定にアクセスする場合:

$var = Application::getInstance()->getConfig()->somevar; 
0

を私はブートストラップの初めに)私は(require_onceをいくつかの場所に短い手を定義しました:

function reg($name, $value=null) { 
    (null===$value) || Zend_Registry::set($name, $value); 
    return Zend_Registry::get($name); 
} 

と私が持っているブートストラップで:

protected function _initFinal() 
{ 
    reg('::app', $this->getApplication()); 
} 

その後、私が使用して任意の場所にアプリケーションのインスタンスを取得することができます。ほとんどのZFのアプリで

$app = reg('::app'); 
3

を、アプリケーションオブジェクトがで宣言されています(ZFW_DISTRIBUTION/bin/zf.shで作成されたアプリのpublic/index.phpを参照してください)。

これはまさにZF方法ではありませんが、$GLOBALS['application']でオブジェクトにアクセスできます。 これはちょっと騙されているように感じますが、演奏後の場合は、これが最も簡単な選択肢になりそうです。

$manager = new My_Model_Manager($GLOBALS['application']->getOption('my')); 
+1

フレームワークとその抽象概念を常に迂回することは可能です。このような解決策は、go-toの解決策ではなく、まったく別の失敗のハックでなければなりません。どのような場合でもパフォーマンスは重要ではありません。 config変数は、パフォーマンスクリティカルなループ内でフェッチする必要はありません。 –

+0

My_Model_Managerとは何ですか? – zardilior

+0

@ zardilior私の推測(4年後)は、仮説的なユーザー定義オプション 'my'に依存する仮説的なユーザーランドクラスです。これらの両方は、SOの質問から賛美されています;-) –

21

バージョン1.8以来、あなたのコントローラに以下のコードを使用することができます。

$my = $this->getInvokeArg('bootstrap')->getOption('my'); 
1
$this->getInvokeArg('bootstrap')->getOptions(); 
// or 

$configDb = $this->getInvokeArg('bootstrap')->getOption('db'); 
0

Aは本当に簡単な方法を直接アクセスすることにより、設定オプションであるにアクセスするには、グローバル$アプリケーションを定義し変数。

class My_UserController extends Zend_Controller_Action { 
    public function indexAction() { 
     global $application; 
     $options = $application->getOptions(); 
    } 
} 
関連する問題