简体   繁体   English

生成URL symfony 2空值

[英]Generate URL symfony 2 empty value

I have this generate url link in symfony 2 : 我在symfony 2中有此生成url链接:

$route = $this->generateUrl('mail', array('from' => $oSender->getSettingValue(),
        'adres' => (empty($aInfo['executorEmail']) ? 'info@blablabla.nl' : $aInfo['executorEmail']),
        'subject' => 'Werkbonnen project ' . $aInfo['projectName'] . ' week ' . $aInfo['workOrderWeeknumber'],
        'name' => $aInfo['executorName'],
        'filename' => $filename,
        'filename2' => '',
        'workOrderId' => $id));

route annotation: 路线注释:

 /**
 * @Route("/workorder/mail/{from}/{adres}/{subject}/{name}/{filename}/{filename2}/{workOrderId}", name="mail")
 * @Template()
 */

And this function where the generate route is going: 而此功能将生成路由:

public function mailAction(Request $request, $from = '', $adres = '', $subject = '', $name = '', $filename = '', $filename2 = '', $workOrderId) {
// some code here not important

}

It keeps saying this : 它一直在说:

Parameter "filename2" for route "mail" must match "[^/]++" ("" given) to generate a corresponding URL.

How can i give a empty value with the generateUrl function in symfony. 我怎样才能用symfony中的generateUrl函数给一个空值。 variable filename2 is optional. 变量filename2是可选的。

Thanks! 谢谢!

Routes are the same as a normal PHP function, that you can't have an empty parameter in the variables passed, when you have other parameters after it. 路由与普通的PHP函数相同,即传递的变量中没有其他参数时,不能在其中包含空参数。
So with your problem you have two choices; 因此,对于您的问题,您有两种选择:

  1. Put the filename2 parameter at the end of the route, making it optional filename2参数放在路径的末尾,使其成为可选
  2. Keep filename2 in place, but give it a default value. filename2保留在原位,但为其提供默认值。

If you choose option 2 an example route would be; 如果选择选项2,则示例路线为:

/**
 * @Route("/workorder/mail/{from}/{adres}/{subject}/{name}/{filename}/{filename2}/{workOrderId}", name="mail", defaults={"filename2" = 0})
 */

More information on routes with parameters can be found here in the Symfony docs . 有关带有参数的路由的更多信息,请参见Symfony文档

You cannot pass an empty string to a URL parameter. 您不能将空字符串传递给URL参数。 Try something like: 尝试类似:

$route = $this->generateUrl('mail', array('from' => $oSender->getSettingValue(),
        'adres' => (empty($aInfo['executorEmail']) ? 'info@blablabla.nl' : $aInfo['executorEmail']),
        'subject' => 'Werkbonnen project ' . $aInfo['projectName'] . ' week ' . $aInfo['workOrderWeeknumber'],
        'name' => $aInfo['executorName'],
        'filename' => $filename,
        'filename2' => 'something',
        'workOrderId' => $id));

The regular expression is clear, [^/]++ means "a non empty string that does not contain the character /". 正则表达式很清楚, [^/]++意思是“一个不包含字符/的非空字符串”。

If that parameter can be empty you cannot put it in the URL, you have to use the query string instead: 如果该参数可以为空,则不能将其放在URL中,而必须使用查询字符串:

/your/really/long/url/with/all/your/parameters?filename2= /你/真的/长/ URL /与/所有/你/参数?文件名2 =

    $route = $this->generateUrl('mail', array(
        'workOrderId' => $id,
        'from' => $oSender->getSettingValue(),
        'adres' => (empty($aInfo['executorEmail']) ? 'info@blablabla.nl' : $aInfo['executorEmail']),
        'subject' => 'Werkbonnen project ' . $aInfo['projectName'] . ' week ' . $aInfo['workOrderWeeknumber'],
        'name' => $aInfo['executorName'],
        'filename' => $filename,
        'filename2' => ''));

Nevermind this is working for me, must give the filename2 parameter as last in the url. 没关系,这为我工作,必须在URL中最后输入filename2参数。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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