Is there a way to get the current URL with current port number in Symfony2? Is there a way to get the current URL with current port number in Symfony2? symfony symfony

Is there a way to get the current URL with current port number in Symfony2?


The Request object holds both URI and port. So from within a Controller you can

public function indexAction(Request $request){    $uri = $request->getUri();    $port = $request->getPort();}

If you're not in a Controller make sure to inject the RequestStack in your class an then fetch uri and port from the master-request

$requestStack->getMasterRequest()->getUri();


Generating an absolute url should include the port.

use Symfony\Component\Routing\Generator\UrlGeneratorInterface;...public function indexAction(Request $request){    $link = $this->generateUrl(        'route_name', [            'route'=>'params'        ],        UrlGeneratorInterface::ABSOLUTE_URL    );    return $this->render('template', [        'link' => $link;    ]);}


You can directly use {{ app.request.uri }} in your twig template.

Ex: If current URI is http://www.example.com:8080/page?q=test&p=2 then {{ app.request.uri }} will return the same string.