sim*_*.ro 11
该Request对象包含URI和端口.所以从控制器中你可以
public function indexAction(Request $request)
{
$uri = $request->getUri();
$port = $request->getPort();
}
Run Code Online (Sandbox Code Playgroud)
如果您不在Controller中,请确保RequestStack在您的类中注入,然后从主请求中获取uri和端口
$requestStack->getMasterRequest()->getUri();
Run Code Online (Sandbox Code Playgroud)
生成绝对 url 应包括端口。
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;
]);
}
Run Code Online (Sandbox Code Playgroud)
{{ app.request.uri }}您可以直接在您的树枝模板中使用。
例如:如果当前 URI 是http://www.example.com:8080/page?q=test&p=2那么{{ app.request.uri }}将返回相同的字符串。