Cake\View\Helper\FormHelper::postLink PHP Метод

    public function postLink($title, $url = null, array $options = [])
    {
        $options += ['block' => null, 'confirm' => null];
        $requestMethod = 'POST';
        if (!empty($options['method'])) {
            $requestMethod = strtoupper($options['method']);
            unset($options['method']);
        }
        $confirmMessage = $options['confirm'];
        unset($options['confirm']);
        $formName = str_replace('.', '', uniqid('post_', true));
        $formOptions = ['name' => $formName, 'style' => 'display:none;', 'method' => 'post'];
        if (isset($options['target'])) {
            $formOptions['target'] = $options['target'];
            unset($options['target']);
        }
        $templater = $this->templater();
        $restoreAction = $this->_lastAction;
        $this->_lastAction($url);
        $action = $templater->formatAttributes(['action' => $this->Url->build($url), 'escape' => false]);
        $out = $this->formatTemplate('formStart', ['attrs' => $templater->formatAttributes($formOptions) . $action]);
        $out .= $this->hidden('_method', ['value' => $requestMethod, 'secure' => static::SECURE_SKIP]);
        $out .= $this->_csrfField();
        $fields = [];
        if (isset($options['data']) && is_array($options['data'])) {
            foreach (Hash::flatten($options['data']) as $key => $value) {
                $fields[$key] = $value;
                $out .= $this->hidden($key, ['value' => $value, 'secure' => static::SECURE_SKIP]);
            }
            unset($options['data']);
        }
        $out .= $this->secure($fields);
        $out .= $this->formatTemplate('formEnd', []);
        $this->_lastAction = $restoreAction;
        if ($options['block']) {
            if ($options['block'] === true) {
                $options['block'] = __FUNCTION__;
            }
            $this->_View->append($options['block'], $out);
            $out = '';
        }
        unset($options['block']);
        $url = '#';
        $onClick = 'document.' . $formName . '.submit();';
        if ($confirmMessage) {
            $options['onclick'] = $this->_confirm($confirmMessage, $onClick, '', $options);
        } else {
            $options['onclick'] = $onClick . ' ';
        }
        $options['onclick'] .= 'event.returnValue = false; return false;';
        $out .= $this->Html->link($title, $url, $options);
        return $out;
    }

Usage Example

Пример #1
0
 /**
  * Creates a link with a surrounding form that submits via POST.
  *
  * This method creates a link in a form element. So don't use this method
  *  in an already opened form.
  *
  * To create a normal link, you should use the `link()` method of the
  *  `HtmlHelper`.
  * @param string $title The content to be wrapped by <a> tags
  * @param string|array|null $url Cake-relative URL or array of URL
  *  parameters or external URL
  * @param array $options Array of options and HTML attributes
  * @return string
  */
 public function postLink($title, $url = null, array $options = [])
 {
     $options = $this->optionsDefaults(['escape' => false, 'title' => $title], $options);
     $options['title'] = trim(h(strip_tags($options['title'])));
     list($title, $options) = $this->addIconToText($title, $options);
     $options = $this->addTooltip($options);
     return parent::postLink($title, $url, $options);
 }