How can i get the absolute url of route with symfony
Using the Routing Component at version 4.0:
<?php
use Symfony\Component\Routing\Generator\UrlGenerator;
UrlGenerator->generate('project_sign_in', [], UrlGenerator::ABSOLUTE_URL);
In Symfony 5.0, if you are using Routing within a service:
namespace App\Service;
use Symfony\Component\Routing\Generator\UrlGeneratorInterface;
.
.
.
private $router;
public function __construct(UrlGeneratorInterface $router)
{
$this->router = $router;
}
public function foo()
{
$this->router->generate('bar', [], urlGeneratorInterface::ABSOLUTE_URL);
}
The last facultative parameter has to be true to generate absolute url:
$router->generate('project_sign_in', array(), true);
in twig:
{{ path('project_sign_in', {}, true) }}
{# or #}
{{ url('project_sign_in') }}
in controller:
$this->generateUrl('project_sign_in', array(), true );
EDIT: for symfony 4, see @Michael B. answerUrlGenerator->generate('project_sign_in', [], UrlGenerator::ABSOLUTE_URL);