php Symfony2 路由:两个可选参数 - 至少需要一个

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/15301546/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-25 09:00:42  来源:igfitidea点击:

Symfony2 Routing: Two optional parameters - at least one required

phpsymfonyrouting

提问by marty

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

我正在尝试在 symfony2 中为以下模式设置一些路由:

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.

我根据symfony2 doc设置了我的 routing.yml 。

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?

不应该工作。有没有办法用 Symfony2 做到这一点?

回答by Hugo Dozois

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

您可以在两条路线中定义它以确保只有 1 个斜线。

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}")

请注意,您可能必须根据您的具体需要更改定义参数的正则表达式。你可以看看这个。复杂的正则表达式必须被". (示例myvar : "[A-Z]{2,20}"

回答by Sébastien

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

要详细说明@Hugo 的答案,请在带有注释的配置下方找到:

/**
 * @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 }

回答by Antonis Charalambous

As per documentation:

根据文档:

http://symfony.com/doc/current/routing/optional_placeholders.html

http://symfony.com/doc/current/routing/optional_placeholders.html

set a default value for optional parameters in the annotations in the controller:

在控制器的注释中为可选参数设置默认值:

/**
* @Route("/blog/{page}", defaults={"page" = 1})
*/
public function indexAction($page)
{
   // ...
}

This way you only need one route in routing.yml

这样你只需要routing.yml中的一个路由