如何在 Symfony2 控制器中获取用户 IP 地址?

如何在 Symfony2 控制器中获取用户 IP 地址?

问题描述:

我需要在提交表单后将评论用户的IP地址存储在数据库中.

I need to store the IP address of the users who comment, in the database after form submission.

有没有symfony2函数来获取IP?或者有什么其他方式获取IP?

Is there any symfony2 function to get the IP? Or any other way to get the IP?

可以使用Request服务获取客户端IP:

You can get the client IP using Request service:

$container->get('request')->getClientIp();