Symfony 2 - 无法得到变量

时间:2013-10-05 18:12:48

标签: php symfony

这是我的路线

homepage:
    pattern: /{name}.{ext}
    defaults:
       _controller:  DprocMainBundle:Index:index
       name: index
       ext: php
    requirements:
       ext: php
       name: index

我想在我的树枝模板中显示{name}。因此控制器获取变量并通过数组发送它

class IndexController extends Controller
{
    public function indexAction($index)
    {
        return $this->render('DprocMainBundle:Dproc:index.html.twig', array('index' => $index));
    }
}

视图

<title>{% block title %}Welcome - {{ name }}{% endblock %}</title>

有错误:

  

控制器   “Dproc \ MainBundle \ Controller \ IndexController :: indexAction()”需要   你为“$ index”参数提供了一个值(因为有   没有默认值,或者之后有一个非可选参数   这个)。

1 个答案:

答案 0 :(得分:1)

在您的控制器中应该看起来像这样:

class IndexController extends Controller
{
    public function indexAction($name)
    {
        return $this->render('DprocMainBundle:Dproc:index.html.twig', array('name' => $name));
    }
}

您应该将变量的名称 - $index更改为$name,因为您已在路线中声明它。此外,您还在Twig中使用它。

相关问题