【发布时间】:2018-08-17 18:42:13
【问题描述】:
我在使用 HtmlHelper 的 CakePHP 2.x 上创建了自己的助手,但它不起作用,这是代码
class NavHelper extends Helper {
public $helpers = array("Html", "Javascript");
function __construct($config = array()) {
}
function link($label, $options) {
if($this->perms(array('controller'=>$options['controller'],'action'=>$options['action']))) {
$html = '<a ';
foreach($options as $label => $value) {
if($label != 'action' || $label != 'controller') {
$html .= " {$label}=\"{$value}\" ";
}
$html .= " href='".$this->Html->url(array('controller'=>$options['controller'],'action'=>$options['action']))."'>".$label."</a>";
}
return $html;
}
return '';
}
致命错误:在非对象上调用成员函数 url() 在 C:\Bitnami\wappstack-5.5.28-0\apache2\htdocs\pokeadmin_v2\app\View\Helper\NavHelper.php 上线 17
p>
但是 $this->Html->url 在 CakePHP 1.3 上完美运行,但在 CakePHP 2.x 上无法运行,还尝试使用 $this->Html = new HtmlHelper; 并出现以下错误:
警告 (4096):参数 1 传递给 HtmlHelper::__construct() 必须是 View 的实例,没有给出, 叫进来 C:\Bitnami\wappstack-5.5.28-0\apache2\htdocs\pokeadmin_v2\app\View\Helper\NavHelper.php 在第 11 行并定义了 [CORE\Cake\View\Helper\HtmlHelper.php, 第 161 行
我还检查了文档,但没有运气。
【问题讨论】: