0

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

2
  • See my EDIT #2 comments. Commented Sep 23, 2017 at 4:12
  • it must work ! if you pass a parametre to path in twig without adding this parameter to the routing it will show you the variable in the url without clean url Commented Sep 23, 2017 at 11:06

3 Answers 3

2

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.

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

6 Comments

Thanks for your reply. I always configure the routing in that way but as you say that doesn't address the URL issue. How can set the value parameter in twig? I know that I can create the url manually, but I'd rather don't do it manually (for example, to avoid having to change the domain in the future)
Yes, I understand what you say. But I haven't any problem passing my values to twig (if I understood you correctly). My problem is generating the url in twig, if I put all in that way I am getting /search/value instead of /search?value=value I need the second option
No it won't equal 'value', if you use let's say getMyValue and that returns an integer. The the URL would be /search/34237, that is if getMyValue returned 34237 as an integer. Do you understand? It doesn't equal value!
Yes, I know that if I pass an entity to my twig template and I have a getter in my entity, I will can access to that value. But in my case I am not passing an entity to my twig template, it is just a string. Let's say that string is "test", so I want the following url: /search?value=test I think maybe I did not put the best values in my example
See my second edit. I have not idea why you would pass in a string? That does not make sense at all! Normally you would pass in a queried result set - which is dynamic. In other words, you do a search and you pass in the search result.
|
0

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)

Comments

0

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

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.