1

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!

3 Answers 3

1

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.

Sign up to request clarification or add additional context in comments.

1 Comment

Thanks this is the best solution for this!
0

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=

Comments

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

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.