简体   繁体   中英

Symfony2 allow empty route parameter “must match ^/ ++ ( given) to generate a corresponding url”

how can I allow empty route parameters?

If name is an empty, its throws the error Parameter "name" for route "article_test" must match "[^/]++" ("" given) to generate a corresponding URL.

My Controller:

/**
 * @param \AppBundle\Entity\Aritcle $article
 * @param string $name
 * @param string $orderNumber
 * @Route("/{id}/test/{name}/{orderNumber}", name="article_test",
 *     defaults={"name"=null, "orderNumber"=null})
 * @return \Symfony\Component\HttpFoundation\RedirectResponse
 */
public function testAction(Aritcle $article, $name = '', $orderNumber = '') 

thanks

Actually there is a way to skip this error, you can make many routes to one methode like this :

/**
* @param \AppBundle\Entity\Aritcle $article
* @param string $name
* @param string $orderNumber
* @Route("/{id}/test/{name}/{orderNumber}", name="article_test", 
* @Route("/{id}/test/{name}", name="article_test_name", 
* @Route("/{id}/test/{orderNumber}", name="article_test_orderNumber", 
* defaults={"name"=null, "orderNumber"=null})
* @return \Symfony\Component\HttpFoundation\RedirectResponse
*/
public function testAction(Aritcle $article, $name = '', $orderNumber = '') 

And then when you want to pass the a route in your controller make sur you passed the right route.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM