Symfony2拆分变量

时间:2014-11-20 21:17:44

标签: php arrays symfony twig

我正在研究一个Symfony2项目,但这个问题可能仍然适用于普通的php。我还在学习。

我有一个需要website.com/2014/fall/other的网址。在symfony的twig模板中,我的链接为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

任何方式重新配置我的数组或以某种方式拆分变量以获得所需的URL?非常感谢提前!

2 个答案:

答案 0 :(得分:1)

您可以使用twig的replace过滤器

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

“Symfony方式”将使用path()来计算给定“季节”和“年”参数的路线

答案 1 :(得分:0)

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

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

控制器:

public function otherAction($year, $semester) {

}