Symfony2 Routing: Two optional parameters - at least one required

22,327

Solution 1

You can define it in two routes to be sure to have only 1 slash.

payment_route_1:
    pattern:  /payment/customer/{customerNumber}/{invoiceNumber}
    defaults: { _controller: PaymentBundle:Index:payment, invoiceNumber: null }
    requirements:
        customerNumber: \d+
        invoiceNumber: \w+
        _method:  GET

payment_route_2:
    pattern:  /payment/customer/{invoiceNumber}
    defaults: { _controller: PaymentBundle:Index:payment, customerNumber: null }
    requirements:
        invoiceNumber: \w+
        _method:  GET

Please note that you might have to change the regex defining the parameters depending of your exact needs. You can look at this. Complex regex have to be surrounded by ". (Example myvar : "[A-Z]{2,20}")

Solution 2

To elaborate on @Hugo answer, please find below the config with annotations :

/**
 * @Route("/public/edit_post/{post_slug}", name="edit_post")
 * @Route("/public/create_post/{root_category_slug}", name="create_post", requirements={"root_category_slug" = "feedback|forum|blog|"})
 * @ParamConverter("rootCategory", class="AppBundle:Social\PostCategory", options={"mapping" : {"root_category_slug" = "slug"}})
 * @ParamConverter("post", class="AppBundle:Social\Post", options={"mapping" : {"post_slug" = "slug"}})
 * @Method({"PUT", "GET"})
 * @param Request $request
 * @param PostCategory $rootCategory
 * @param Post $post
 * @return array|\Symfony\Component\HttpFoundation\RedirectResponse
 */
public function editPostAction(Request $request, PostCategory $rootCategory = null, Post $post = null)
{ Your Stuff }
Share:
22,327
marty
Author by

marty

Updated on April 06, 2020

Comments

  • marty
    marty about 4 years

    I'm trying to set up some routes in symfony2 for the following pattern:

    www.myaweseomesite.com/payment/customer/{customernumber}/{invoicenumber}
    

    Both parameters are optional - so the following scenarios must work:

    www.myaweseomesite.com/payment/customer/{customerNumber}/{invoiceNumber}
    www.myaweseomesite.com/payment/customer/{customerNumber}
    www.myaweseomesite.com/payment/customer/{invoiceNumber}
    

    I set up my routing.yml according to the symfony2 doc.

    payment_route:
    pattern:  /payment/customer/{customerNumber}/{invoiceNumber}
    defaults: { _controller: PaymentBundle:Index:payment, customerNumber: null,  invoiceNumber: null }
    requirements:
        _method:  GET
    

    This works great so far. The problem is, that if both parameters are missing or empty, the route should not work. So

    www.myaweseomesite.com/payment/customer/
    

    should not work. Is there any way to do this with Symfony2?

  • Hugo Dozois
    Hugo Dozois about 11 years
    @marty glad I could help! To give a bit more info, the first route matches your 2 first types. And the second one the third type. (oops I forgot to removed the customerNumber: null from the first route, else it will accept the route without any parameters. I've updated to reflect that!)