Thomas Crawford
Thomas Crawford

Reputation: 896

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']) ? '[email protected]' : $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!

Upvotes: 1

Views: 909

Answers (3)

Rooneyl
Rooneyl

Reputation: 7902

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.

Upvotes: 1

Thomas Crawford
Thomas Crawford

Reputation: 896

    $route = $this->generateUrl('mail', array(
        'workOrderId' => $id,
        'from' => $oSender->getSettingValue(),
        'adres' => (empty($aInfo['executorEmail']) ? '[email protected]' : $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.

Upvotes: 0

ggioffreda
ggioffreda

Reputation: 650

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']) ? '[email protected]' : $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=

Upvotes: 0

Related Questions