0

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!

2 Answers 2

1

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

Sign up to request clarification or add additional context in comments.

1 Comment

Yes, I guess I should probably do this the proper way and pass the variables through the symfony path(). Good call, and thanks for the documentation.
0

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) {

}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.