Symfony Tasks: Getting the output of a module / action
开发者_高级运维Is it possible to capture the output of an action in a task?
Ex:
FooAction
{
executeIndexSuccess()
}
and in my Task:
FooAction->Index
Is this possible?
Yes. In your task's execute()
method:
sfContext::createInstance($this->configuration);
$output = sfContext::getInstance()->getController()->getPresentationFor("module", "action");
The resultant output from the action and associated view will be stored in $output
.
If your action needs variables setting into the session, you can also do things like:
sfContext::getInstance()->getUser()->setAttribute("mySessionVar", 123);
sfContext::getInstance()->getUser()->setFlash("myFlashVar", "foo");
before you call getPresentationFor()
, so that the variables are present when the action is executed.
You should not do this in a task! Controller actions are not build for such purpose. Just refactor your Controller. Extract the code you need to share with the task to a seperate class and use this class in your action and the task! Keep in mind that the sfContext is not a reliable source of information. The content of this object depends on how your application is started (Commandline != HTTP)
精彩评论