Is there a function in Symfony 2.7 which returns the current absolute URL with the current port number?
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With