2013-10-05 36 views
0

這是我的路線Symfony的2 - 不能得到變量

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

,我想在我的樹枝模板來顯示(名稱)。所以控制器得到變量,並通過數組將其發送

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> 

有錯誤:

Controller "Dproc\MainBundle\Controller\IndexController::indexAction()" requires that you provide a value for the "$index" argument (because there is no default value or because there is a non optional argument after this one).

+2

將'$ index'更改爲'$ name' – tttony

回答

1

在你的控制器應該是這樣的:

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

你應該c將變量的名稱 - $index更改爲$name,因爲您在路由中聲明瞭它。而且,你也在枝條中使用它。

+0

是的,我已經做到了。 –