简体   繁体   中英

Set $this->view->plugin for zf2 php

I am using the zf2 flashMessenger() to output Google Analytics snippet to register a transaction. The flashMessenger allows me to output the snippet only once and avoids duplicate events.

The issue is that flashMessenger uses the EscapeHtml helper to escape the content before outputting it. This is the getter from the flashMessenger.

   /**
     * Retrieve the escapeHtml helper
     *
     * @return EscapeHtml
     */
    protected function getEscapeHtmlHelper()
    {
        if ($this->escapeHtmlHelper) {
            return $this->escapeHtmlHelper;
        }

        if (method_exists($this->getView(), 'plugin')) {
            $this->escapeHtmlHelper = $this->view->plugin('escapehtml');
        }

        if (!$this->escapeHtmlHelper instanceof EscapeHtml) {
            $this->escapeHtmlHelper = new EscapeHtml();
        }

        return $this->escapeHtmlHelper;
    }

One of the condition says that if method_exists($this->getView(), 'plugin') , it uses $this->view->plugin('escapehtml'); . How do I set $this->view->plugin because I want to use a custom EscapeHtml() object.

Thanks!

You can just override the default escapeHtml view helper with your own if you don't have need for it:

In your Module#getViewHelperConfig method:

class Module 
{
     public function getViewHelperConfig() 
     {
         return array(
             'invokables' => array(
                 'escapeHtml' => 'Foo\View\Helper\EscapeHtml',
             )
         );
     }
 }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM