Magento 2 Documentation  2.3
Documentation for Magento 2 CMS v2.3 (December 2018)
FormKey.php
Go to the documentation of this file.
1 <?php
7 
12 class FormKey
13 {
17  const FORM_KEY = '_form_key';
18 
22  protected $mathRandom;
23 
27  protected $session;
28 
33  protected $escaper;
34 
40  public function __construct(
41  \Magento\Framework\Math\Random $mathRandom,
42  \Magento\Framework\Session\SessionManagerInterface $session,
43  \Magento\Framework\Escaper $escaper
44  ) {
45  $this->mathRandom = $mathRandom;
46  $this->session = $session;
47  $this->escaper = $escaper;
48  }
49 
55  public function getFormKey()
56  {
57  if (!$this->isPresent()) {
58  $this->set($this->mathRandom->getRandomString(16));
59  }
60  return $this->escaper->escapeHtmlAttr($this->session->getData(self::FORM_KEY));
61  }
62 
66  public function isPresent()
67  {
68  return (bool)$this->session->getData(self::FORM_KEY);
69  }
70 
75  public function set($value)
76  {
77  $this->session->setData(self::FORM_KEY, $value);
78  }
79 }
$value
Definition: gender.phtml:16
__construct(\Magento\Framework\Math\Random $mathRandom, \Magento\Framework\Session\SessionManagerInterface $session, \Magento\Framework\Escaper $escaper)
Definition: FormKey.php:40