2014-11-20 63 views
0

我正在开发一个Symfony2项目,但这个问题可能仍然适用于一般的PHP。我仍然非常了解。Symfony2拆分变量

我有一个网址,需要像website.com/2014/fall/other。在symfony里的树枝模板我有联系的website.com/{{ allSemesters }}/other

我传递中的变量是

$allSemesters=array("Fall 2014", "Spring 2015", "Summer 2015"); 

这是我的模板。

{% for allSemester in allSemesters %} 
    <a href="{{ allSemester }}/other">{{ allSemester | capitalize }}</a> 
{% endfor %} 

在去链接产生的:website.com/Spring%202015/other而不是website.com/2015/Spring/other

任何方式来重新配置我的数组或分裂变量莫名其妙得到所需的网址?提前谢谢了!

回答

1

你可以用树枝的replace过滤

<a href="{{ allSemester |replace({' ': "/"}) }}/other"> 

的“Symfony的方式”是使用path()计算给出一个“赛季”和“年”参数

+0

是的,我想我可能应该这样做,并通过symfony路径()传递变量。好的电话,谢谢你的文档。 – Moose 2014-11-20 23:48:10

0

使用.yml路由的路由,我会做这样的事情:

semester_other: 
    pattern: /{year}/{semester}/other/ 
    defaults: { _controller: "AppBlablaBundle:Semester:other", year: null, semester: null} 

而且控制器:

public function otherAction($year, $semester) { 

}