0

フロントエンド アプリのすべてのアクションの前にいくつかのアクション (php スクリプト) を作成し、そのスクリプトの結果を変数のアクションに渡したいので、すべてのアクションから変数値を取得できます。このように sth をどこで宣言すればよいですか?

4

2 に答える 2

3

フィルター ソリューションがニーズに合わない場合は、preExecute 関数を使用して基本アクション クラスを作成することもできます。

// app/frontend/lib/baseActions.class.php

class baseActions extends sfActions
{
   public function preExecute()
   {
      $this->myVar = .... // define your vars...
   }
}

次に、モジュール アクション クラスが baseActions クラスを拡張します。

// app/frontend/modules/myModule/actions/actions.class.php

class myModuleActions extends baseActions
{
   public function executeIndex(sfWebRequest $request)
   {
      // var $this->myVar is available in any action and in your template
      ... 
   }
}

モジュール クラス アクションで preExecute 関数を使用する必要がある場合は、忘れずに呼び出しparent::preExecute()てください。

于 2012-04-19T10:14:07.420 に答える
2

どのような情報ですか?

フィルターを使用することをお勧めします。

あなたのapps/frontend/config/filters.yml

rendering: ~
myfilter:
  class: myCustomFilter

ファイルを作成しますlib/filter/myCustomFilter.php

<?php
class myCustomFilter extends sfFilter
{
  public function execute ($filterChain)
  {
    if ($this->isFirstCall())
    {
      // do what ever you want here.
      $config = Doctrine_Core::getTable('Config')->findAll();
      sfConfig::set('my_config', $config);
    }

    $filterChain->execute();
  }
}

そして、どこからでもデータを取得できます。

sfConfig::get('my_config');
于 2012-04-18T14:12:07.200 に答える