Reputation: 529
Maybe this is a stupid question but I spent a lot of time trying to make it working...
This is from my routing file:
search:
path: /search
defaults: { _controller: MyAppBundle:Default:search}
My twig template:
{{ path('search', {'value': value}) }}
And my controller:
public function searchAction(Request $request){
$value = $request->query->get('value');
My problem is the following, with the above data I am generating this url:
/search/value
instead of the url which I want:
/search?value=value
I prefer clean urls, but I need to pass the values in the url using "?" due to that I need to pass different values in the url and some of them can be omitted in different circumstances
Upvotes: 0
Views: 17824
Reputation: 1
Your Controller will be like:
#[Route('/place_payment/{id}', name: 'place_payment', methods: ['GET', 'POST'])]
public function addPayment(Request $request, Place $place, int $id): Response
{
$idPayment = (int) $request->query->get('idPayment');
And your TWIG will be like:
{% for payment in existingPayments %}
<a href="{{ path('user_place_payment', {'id': place.id, 'idPayment': payment.id }) }}">{{ payment.name }} - delete this payment method</a><br><br>
{% endfor %}
{% for payment in missingPayments %}
<a href="{{ path('user_place_payment', {'id': place.id, 'idPayment': payment.id }) }}">{{ payment.name }} - add this payment method</a><br><br>
{% endfor %}
So, you get URL like your_server/place_payment/103?idPayment=3
Upvotes: 0
Reputation: 529
If you face this issue, please check that you aren't passing your parameters in your routing file and also check that you haven't a form in your html with post as method (if you have any form with post method, changing it to get will solve the issue)
Upvotes: 0
Reputation: 7764
Why not do this instead:
search:
path: /search/{value}
defaults: { _controller: MyAppBundle:Default:search}
public function searchAction(Request $request, $value){
// Do something with $value...
It doesn't address the URL issue, but you can easily set the value parameter in your Twig file, and this is the standard way to do it in Symfony.
EDIT #2 - based on comment.
What I do is something like this:
{{ path('searchPet',{'petID': pet.getPetId}) }}
In the above case, searchPet
is the route, petID
is the parameter name, and pet.getPetId
is my pet
method; where pet
is a Petition Entitiy that I've passed in to the Controller (which Twig renders); So I'm calling getPetId()
which returns the Id of the Petition.
So if you render in Controller like this with param
being the variable:
return $this->render('search.html.twig', array(
'param' => 'test',
...
));
Then in Twig do this:
{{ path('search', {'value': param}) }}
Then URL will be in this case: /search/test
EDIT #3 - based on last comment.
Quote the value you want to send, so you want to send 'test', do this:
{{ path('search', {'value': 'test'}) }}
I tested this - it works.
Upvotes: 1