php - A function call as argument in Symfony/Twig path function
Get the solution ↓↓↓I have a route like this:
/**
* @Route("/admin/user/edit/{id}", defaults={"id" = 0}, name="admin_user_edit")
* @Route("/admin/user/edit")
*
*/
public function editAction(Request $request, $id = 0){
(...)
And then a Twig template, where I list all users:
{% for user in users %}
<li>
<a href='{{ path('admin_user_edit'),{'id' : user.getId()} }}'>
{{ user.getUsername() }}
</a>
</li>
{% endfor %}
As you can see I'm trying to pass the 'user.getId()' as an id. And I get this:
Unexpected token "punctuation" of value "," ("end of print statement" expected) in admin/users/list.html.twig at line 5.
I know it's about this user.getId() because everything was fine before I added this piece of code.
So how can I pass a function result to this "path" function?
Answer
Solution:
Your path function should have the route name and the parameters within the parenthesis like
{{ path('admin_user_edit', {'id' : user.getId() }) }}
Also, as you aren't using arguments in youruser.getId()
call you can just useuser.id
instead.
Share solution ↓
Additional Information:
Link To Answer People are also looking for solutions of the problem: call to undefined function mysqli_connect()
Didn't find the answer?
Our community is visited by hundreds of web development professionals every day. Ask your question and get a quick answer for free.
Similar questions
Find the answer in similar questions on our website.
Write quick answer
Do you know the answer to this question? Write a quick response to it. With your help, we will make our community stronger.