symfonyのforwardしたときにテンプレートに値を渡すのは?
symfonyではテンプレートに値を渡すのは簡単だ。action.class.phpで$thisに値を設定すればいい
class sampleActions extends sfActions
{
/**
* Executes index action
*
*/
public function executeIndex()
{
$this->expample[] = "あははは";
$this->expample[] = "いひひひ";
$this->expample[] = "うふふふ";
$this->expample[] = "えへへへ";
$this->expample[] = "おほほほ";
}
}テンプレで呼び出すには、
module/sample/templates/indexSucess.php
<?php var_dump( $example ); ?>
でいい。とてもわかりやすい。
$this->forwardが入るとそうもいかない。
actions.class.php内部でforward。すると、設定してた値は破棄されるようだ。テンプレートから呼び出すときに不便だ。
破棄される例。
action.class.phpでこのように定義して、これをテンプレートから呼び出す。
class sampleActions extends sfActions
{
/**
* Executes index action
*
*/
public function executeIndex()
{
}
public function executeList()
{
$this->post = PostPeer::retrieveByPk($this->getRequestParameter('id'));
if( $this->post ){
$c = new Criteria();
$c->add(CommentPeer::POST_ID, $this->getRequestParameter('id'));
$c->addAscendingOrderByColumn(CommentPeer::CREATED_AT);
$this->comments = CommentPeer::doSelect($c);
}else{
$this->message = "エントリが見つかりませんでした"; #fowardすると消える
$this->forward("sample", "index");
}
}
}executeIndexに対応した、templateファイル、indexSuccess.phpでは
module/sample/templates/indexSucess.php
<?php var_dump( $message ); ?>
これをブラウザから実行するとNULL
Notice: Undefined variable: testA in /.../indexSuccess.php on line 2 NULL
となり、値が取得できない。困った。