8

I need to generate full URL with symfony, for now I have :

$this->generateUrl('my_route', array('type' => 'param')

This function generate something like : /my_project/xxxxxxxxx

Is there a function like generateUrl for generate a fully URL ? I need the HTTP and the domain name.

I know I can add them manually but If there is a function for that it's better.

Thanks

Clément Andraud
  • 9,103
  • 25
  • 80
  • 158
  • Does this answer your question? [How can i get full url to include in newsletter sent with Symfony2?](https://stackoverflow.com/questions/10621068/how-can-i-get-full-url-to-include-in-newsletter-sent-with-symfony2) – A.L Jan 10 '20 at 15:34

2 Answers2

21

It is quite easy:

$this->generateUrl('my_route', array('type' => 'param'), UrlGeneratorInterface::ABSOLUTE_URL);

Don't forget to add:

use Symfony\Component\Routing\Generator\UrlGeneratorInterface;
Michaël Perrin
  • 5,903
  • 5
  • 40
  • 65
0

This is do it:

   $this->generateUrl('my_route',  array('type' => 'param'), true);
MistaJase
  • 839
  • 7
  • 12
  • What about the symfony-devs chaning the api, and as such the value of `UrlGeneratorInterface::ABSOLUTE_URL`? Interfaces exist for a reason. – Yoshi Jan 15 '16 at 13:33
  • this option was for Symfony versions before 2.7 – gp_sflover Jan 15 '16 at 13:33
  • Indeed - this option was for older versions of symfony. I just checked the latest docs – MistaJase Jan 15 '16 at 13:37
  • I'm using Symfony3 and it's ok – Clément Andraud Jan 15 '16 at 13:42
  • 1
    Even before Symfony 2.7 using the constants did work and should always be preferred (starting with Symfony 2.8 not using them will trigger deprecations and in Symfony 3.0 the constant values have been changed to be integers. – xabbuh Jan 15 '16 at 13:46