2

I try to generate URL with Twig in my Symfony2 project. My URL take Boolean as parameters, when I put True in the twig it's work but when I put false an error is throw.

An exception has been thrown during the rendering of a template ("Parameter "boolean" for route "route_1" must match "[^/]++" ("" given).

{{ path('route_1', {'id': item.id, 'boolean': false }) }}

I haven't any requirement on this parameter.

    /**
     * @Route("route1-{id}/boolean{boolean}", requirements={"id" = "\d+"}, name="route_1")
     */
    public function Route1Action($id, $boolean, Request $request) {
         //Some stuff
    }

Edit

So, use Boolean in URL is bad. I put number and php convert automatically in boolean. 0 is false and all other numbers is true.

<?php
var_dump((bool) "");        // bool(false)
var_dump((bool) 1);         // bool(true)
var_dump((bool) -2);        // bool(true)
var_dump((bool) "foo");     // bool(true)
var_dump((bool) 2.3e5);     // bool(true)
var_dump((bool) array(12)); // bool(true)
var_dump((bool) array());   // bool(false)
var_dump((bool) "false");   // bool(true)
?>

http://php.net/manual/en/language.types.boolean.php

With this code it's work :

Action :

/**
 * @Route("route1-{id}/boolean{boolean}", requirements={"id" = "\d+", "boolean" = "\d+"}, name="route_1")
 */
public function Route1Action($id, $boolean, Request $request) {
     //Some stuff
}

The url to generate :

{{ path('route_1', {'id': item.id, 'boolean': 0 }) }} //To send false
{{ path('route_1', {'id': item.id, 'boolean': 1 }) }} //To send true (1 or any other numbers)

Thanks for your help. :)

1
  • Please show your action.
    – Isky
    Apr 5, 2016 at 8:08

2 Answers 2

3

It kind of depends on what exactly you want to have in the URL. Boolean variables are generally not a good idea, since URL is a string.

Will the boolean variable be represented as true or as 1 ?

{{ path('route_1', {'id': item.id, 'boolean': 'false' }) }}

or

{{ path('route_1', {'id': item.id, 'boolean': 1 }) }}

You also should specify the requirements for the argument, in that case I can sent anything.

Anyway, you should pass always text-representable variables like numeric types or text.

1
<a href="{{ path('route_1', {'id': item.id, 'boolean': 'false' }) }}">Link</a>
0

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.