3

I have a route with multiple parameters and I want to define default values when it's empty. I didn't find something similar on SO...

My Route (yml) :

app_product_show_range_tag:
path: 'list-range{range_id}-tag{tag_id}-{name_slug}/{page}'
methods: 'GET'
defaults:
    _controller: 'AppBundle:Product:showRangeTag'
    page: 1
    tag_id: 2
    range_id: null
requirements:
    name_slug: '([a-zA-Z0-9]+(?:-[a-zA-Z0-9]+)*)'
    tag_id: '\d+'
    range_id: '\d+'
    page: '\d+'

My Controller (begin ) :

/**
 * Show a range
 * @ParamConverter("productRange", options={
"mapping": {"range_id": "productRangeId", "locale": "locale"},
"repository_method": "findOneById",
"map_method_signature" = true
})
 *     @ParamConverter("productTag", options={
"mapping": {"tag_id": "productTagId", "locale": "locale"},
"repository_method": "findOneById",
"map_method_signature" = true
})
 */
public function showRangeTagAction(ProductRange $productRange = null, ProductTag $productTag = null, string $name_slug, int $page)
{ [...]

I don't understand what is wrong... I defined all default values, the controller too.

I did a quick demo at the url : https://streamable.com/1paw

Don't hesitate to ask more information !

2 Answers 2

1

For what I can see your requirements regexp are wrong, or do not match what you're trying to do on your demo

requirements:
name_slug: '([a-zA-Z0-9]+(?:-[a-zA-Z0-9]+)*)'
tag_id: '\d+'
range_id: '\d+'
page: '\d+'

\d+ means that you must provide at least a number. This explains the 404 response code you get on your demo. Use of \d* will solve this. You'll be allowed to use the url you're typing.

One more thing, unrelated. In PHP you're supposed to provide first the arguments without default values, as stated here : php manual

Hope it helps you.

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

2 Comments

I know this, but for example with the number of page (another route), I had the same and it is working, SF3 looks to understand if there a number of page or not. But when I have more optional, nothing is working. (it is not easy to explain)
Hi, just for my curiosity if you try this, what's happening ? public function showRangeTagAction(ProductRange $productRange, ProductTag $productTag, string $name_slug, int $page)
0

strange because your optionnal parameter /{page} work fine in your exemple ? And it's first parameter set So try this, and change order parameter, maybe ...

defaults:  { _controller: 'AppBundle:Product:showRangeTag', page: 1, tag_id: 2, range_id: null}

good luck

2 Comments

Nop, same issue :(
humm you have just this url in your yml ? maybe another url conflict your url if she'is call before ? try to deplace your url on the top in your yml file

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.