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->request = $request;
}
public function bar() {
if ($ip == $this->request->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".
@request
, you need to use the service called@request_stack
, and callgetCurrentRequest()
on the injected RequestStack object. – phayes Aug 15 '17 at 18:44