简体   繁体   中英

Generate URL symfony 2 empty value

I have this generate url link in symfony 2 :

$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. variable filename2 is optional.

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.
So with your problem you have two choices;

  1. Put the filename2 parameter at the end of the route, making it optional
  2. Keep filename2 in place, but give it a default value.

If you choose option 2 an example route would be;

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

You cannot pass an empty string to a URL parameter. 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:

/your/really/long/url/with/all/your/parameters?filename2=

    $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.

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