0

在这里,我看到有人建议扩展Zend\View\Helper\Escaper\AbstractHelper;,结果是这样的:

namespace Photo\View\Helper;

use Zend\View\Helper\Escaper\AbstractHelper;

class Imghelper extends AbstractHelper
{
protected $_baseUrl = null;
protected $_exists = array();

public function __construct ()
{
    $url = PUBLIC_PATH;
    $root = '/' . trim($url, '/');

    if ($root == "/")
      {$root = "";}

    $this->_baseUrl = $root . "/";
}

public function escape($value)
{
   return $this->getEscaper()->escapeHtml($value);
}

public function __invoke($path, $params = array())
{
           $plist = array();
    $str = null;

    $imgpath = $this->_baseUrl . ltrim($path);

    if (!isset($this->_exists[$path]))
      {$this->_exists[$path] = file_exists(realpath($imgpath));}

    if (!isset($params['alt']))
      {$params['alt'] = '';}

    foreach ($params as $param => $value)
      {$plist[] = $param . '="' . $this->escape($value) . '"';}

            $str = " ".join(" ", $plist);
    $p = ($this->_exists[$path]) ? $this->_baseUrl.ltrim($path,'/') : null;

    if (isset($p))
      {return '<img src="'.$p.'" '.$str.' />';}
}


} 

但是,当我在我的代码中尝试它时,我得到一个错误

Strict Standards: Declaration of Application\View\Helper\Imghelper::__invoke() should be compatible with Zend\View\Helper\Escaper\AbstractHelper::__invoke($value, $recurse = 0) in ...

错误消息可以出错吗?或者有没有更好的方法来做到这一点?谢谢!

4

1 回答 1

2

我不建议扩展 Abstract Escape 帮助器,因为您没有编写转义帮助器。坚持你原来的计划,只是扩展Zend\View\Helper\AbstractHelper。然后,您可以通过访问逃生助手。$this->view->escapeHtml()$this->view->escapeJs()等等。

于 2013-10-30T15:44:33.690 回答