在这里,我看到有人建议扩展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 ...
错误消息可以出错吗?或者有没有更好的方法来做到这一点?谢谢!