首页 > 解决方案 > 如何访问服务变量?

问题描述

public function pages($slug, PagesGenerator $pagesGenerator)
{
    $output = $pagesGenerator->getPages($slug);

    $id = $page->getId();

    return $this->render('list.html.twig', [ 'output' => $output,  'id' => $id]);
}

但我得到了错误:

注意:未定义变量:page

标签: phpsymfonyscope

解决方案


您的新控制器显然没有template变量到变量列表中,只是output. 看起来您的正确代码应该是:

  public function pages($slug, PagesGenerator $pagesGenerator)
  {
      $output = $pagesGenerator->getPages($slug);

      return $this->render('list.html.twig', $output);
  }

更新:更新问题的答案基本相同,您需要做的就是将模板变量作为普通数组传递

return $this->render('list.html.twig', array_merge($output, ['table' => $table]));

或根据模板中的实际结构使用它们,例如,{{ output.page }}而不是{{ page }}


推荐阅读