Moose
Moose

Reputation: 1241

Symfony2 Split Variable

I'm working on a Symfony2 project, but this question probably still applies to just general php. I'm still very much learning.

I have a url that needs to like website.com/2014/fall/other. In symfony's twig template I have the links as website.com/{{ allSemesters }}/other

The variables I'm passing in are

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

This is in my template.

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

Resulting in links that go to: website.com/Spring%202015/other and not website.com/2015/Spring/other.

Any way to reconfigure my array or split the variable somehow to get the desired url? Many thanks in advance!

Upvotes: 0

Views: 375

Answers (2)

Lauri Elias
Lauri Elias

Reputation: 1299

Using .yml routing, I would do something like this:

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

And the controller:

public function otherAction($year, $semester) {

}

Upvotes: 0

Paul Dixon
Paul Dixon

Reputation: 300855

You could use twig's replace filter

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

The "Symfony way" would be to use path() to calculate the route given a 'season' and 'year' parameter

Upvotes: 1

Related Questions