【问题标题】:How do i reference whatever content is being sent in this response?我如何引用此响应中发送的任何内容?
【发布时间】:2016-08-09 19:20:24
【问题描述】:

我目前在一个系统中工作,该系统早在我进入这家公司之前就建​​立在 symfony 1.4 上。

我正在尝试修改强制下载 excel 的功能,但我无法确定内容的设置位置。我的假设是它以某种方式抓取了整个 '$this' 对象?

我看到了标题:

$this->getResponse()->setHttpHeader('Content-Disposition', 'filename=winners.xls');
$this->getResponse()->setHttpHeader('Content-Type', 'application/vnd.ms-excel; charset=utf-8');

我看到这个函数被调用了:

$this->setLayout('none');

其他所有内容都是传递给视图的变量。

我只需要能够引用正在发送以供下载的内容,以便我可以通过 PHPExcel 运行它。

知道如何引用正在发送的内容吗?

更新:这是整个函数

$round = $request->getParameter("round");
$league = $request->getParameter("league");

$this->all_contest_ids = array();
$contest = asPickem::getInstance()->getContest();

// find all child contest ids
$this->all_contest_ids[] = $contest->getId();
if ($contest->getNode()->hasChildren()) {
  foreach ($contest->getNode()->getDescendants() as $child) {
    $this->all_contest_ids[] = $child->getId();
  }
}


if (!$round || $round == "-1") {
  $tablename = ucfirst(asPickem::getInstance()->getProduct()->getSlug()) . 'RankUserOverall';
  $query = Doctrine_Core::getTable($tablename)->getRankingsQuery(asPickem::getInstance()->getContest()->getId(), $this->getExcludedUserEntryIds());
  $this->title = "Overall Rankings";
} // show round results
else {

  if (isset($league)) {
    $tablename = ucfirst(asPickem::getInstance()->getProduct()->getSlug()) . 'RankLeagueUser';
    $query = Doctrine_Core::getTable($tablename)->getRankingsQuery($league, $round);

  } else {
    $tablename = ucfirst(asPickem::getInstance()->getProduct()->getSlug()) . 'RankUser';
    $query = Doctrine_Core::getTable($tablename)->getRankingsQuery(asPickem::getInstance()->getContest()->getId(), $round, $this->getExcludedUserEntryIds($this->round));

  }


  $r = Doctrine_Core::getTable('CampaignRound')->find($round);

  $this->title = $r->getView() . " Rankings";
}

$limit = $request->getParameter("limit", 100);
if ($limit > 0) {
  $query->limit($limit);
}

$this->rankings = $query->execute();

// load survey questions
$survies = array();
$thisSurvies = Doctrine_Core::getTable('Survey')->getByContest(asPickem::getInstance()->getContest()->getId());

foreach ($thisSurvies as $survey) {
  $survies[] = $survey;
}

// if this contest is a child of a portal then include the parent surveys
$c = asPickem::getInstance()->getContest();
while ($c->getParent()) {
  $c = $c->getParent();

  if ($c->getProduct()->getSlug() == 'portal') {
    $this->includeParentSurveyQuestions = true;
    break;
  }
}


if ($this->includeParentSurveyQuestions) {
  $contest = asPickem::getInstance()->getContest();
  while ($contest->getParent()) {
    $contest = $contest->getParent();
    $parentSurvies = Doctrine_Core::getTable('Survey')->getByContest($contest->getId());

    foreach ($parentSurvies as $survey) {
      $survies[] = $survey;
    }
  }
}

$this->questions = array();
foreach ($survies as $survey) {
  foreach ($survey->getSortedQuestions() as $question) {
    $this->questions[$question->getId()] = $question->getQuestionText();
  }
}
unset($survies);

$temp = tempnam(sys_get_temp_dir(), 'html');
file_put_contents($temp, $this->getResponse()->getTemplate());

//build instance of PHPExcel object, and load/read html file
require_once dirname(__FILE__) . '/../../../../../EXCEL/PHPExcel.php';
$objPHPExcel = new PHPExcel();
$excelHTMLReader = PHPExcel_IOFactory::createReader('HTML');
$excelHTMLReader->loadIntoExisting($temp, $objPHPExcel);
$objPHPExcel->getActiveSheet()->setTitle('userlist');

//delete the temporary file
unlink($temp);

$this->getResponse()->setHttpHeader('Content-Disposition', 'filename=winners.xls');
$this->getResponse()->setHttpHeader('Content-Type', 'application/vnd.ms-excel; charset=utf-8');

$writer = PHPExcel_IOFactory::createWriter($objPHPExcel, 'Excel2007');
$this->renderText($writer->save('php://output'));

$this->setLayout('none');

【问题讨论】:

    标签: php phpexcel symfony-1.4


    【解决方案1】:

    我能想通了,答案如下……

    这就是我能够引用发送到视图的信息的方式:

    $this->setLayout('none');
    $view = $this->getController()->getView($this->getContext()->getModuleName(), $this->getContext()->getActionName(), sfview::SUCCESS);
    $view->execute();
    $view->getAttributeHolder()->add($this->getVarHolder()->getAll());
    $exContent = $view->render();
    

    对于那些可能最终面临类似问题的人,这里是我用来获取参考的全部内容,并通过 PHPExcel 运行它以停止有关文件及其的 Excel 警报弹出窗口:

    $this->setLayout('none');
    $view = $this->getController()->getView($this->getContext()->getModuleName(), $this->getContext()->getActionName(), sfview::SUCCESS);
    $view->execute();
    $view->getAttributeHolder()->add($this->getVarHolder()->getAll());
    $exContent = $view->render();
    
    $temp = tempnam(sys_get_temp_dir(), 'html');
    file_put_contents($temp, $exContent);
    
    //build instance of PHPExcel object, and load/read html file
    require_once dirname(__FILE__) . '/../../../../../EXCEL/PHPExcel.php';
    $objPHPExcel = new PHPExcel();
    $excelHTMLReader = PHPExcel_IOFactory::createReader('HTML');
    $excelHTMLReader->loadIntoExisting($temp, $objPHPExcel);
    $objPHPExcel->getActiveSheet()->setTitle('userlist');
    
    //delete the temporary file
    unlink($temp);
    
    header('Content-Type: application/vnd.ms-excel'); // header for .xls file
    header('Content-Disposition: attachment;filename=winners.xls'); // specify the download file name
    header('Cache-Control: max-age=0');
    
    $writer = PHPExcel_IOFactory::createWriter($objPHPExcel, 'Excel5');
    $writer->save('php://output');
    
    $this->setLayout(false);
    

    希望这能够帮助某人。谢谢!

    【讨论】:

      猜你喜欢
      • 2013-01-16
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-09-04
      • 2021-06-29
      相关资源
      最近更新 更多