[英]In ZF2 how to Escape HTML in Custom View Helpers
在這里 ,我看到有人建議擴展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 ...
是否可以錯誤顯示錯誤消息? 還是有更好的方法來做到這一點? 謝謝!
我不建議擴展Abstract Escape幫助程序,因為您不是在編寫Escape幫助程序。 堅持原始計划,然后擴展Zend\\View\\Helper\\AbstractHelper
。 然后,您可以通過來訪問逃生助手。 $this->view->escapeHtml()
, $this->view->escapeJs()
等。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.