How to get ip address? I used the following code:

services:
  foo:
    class: Drupal\MYMODULE\Foo
    arguments: ['@request']


In MYMODULE/src/Foo.php

namespace Drupal\MYMODULE;

use Symfony\Component\HttpFoundation\Request;

class Foo {
  /**
   * @var \Symfony\Component\HttpFoundation\Request
   */
  protected $request;

  /**
   * Constructs a Foo object.
   *
   * @param Symfony\Component\HttpFoundation\Request $request
   *   The request object.
   */
  public function __construct(Request $request) {
    $this-&gt;request = $request;
  }

  public function bar() {
    if ($ip == $this-&gt;request-&gt;getClientIp()) {
      drupal_set_message('ip', t('You may not ban your own IP address.'));
    }
  }

}


but this gives me error:


  Symfony\Component\DependencyInjection\Exception\ServiceNotFoundException'
  with message 'The service "foo" has a dependency on a non-existent
  service "request".
According to Get the user IP address and Symfony HttpFoundation you can get visitor IP by 

first add use Symfony\Component\HttpFoundation; then 

 \Drupal::request()-&gt;getClientIp()