CakePHP 3 自定义助手不工作
CakePHP 3 Custom helper not working
你好,我正在尝试实现一个扩展 Html 助手的助手。
我根据文档创建了以下空的自定义助手
<?php
namespace App\View\Helper;
use Cake\View\Helper;
use Cake\View\Helper\HtmlHelper;
use Cake\View\View;
/**
* MyHtml helper
*/
class MyHtmlHelper extends HtmlHelper
{
public function __construct(View $view, $config = []) {
parent::__construct($view, $config);
}
/**
* Default configuration.
*
* @var array
*/
protected $_defaultConfig = [];
}
我将它们都加载到 AppController 上,但是当我在视图中使用它们时,我从 Html 助手而不是自定义助手收到了预期结果,为什么? (第二个助手没有 return 一个错误,只是一个空结果)
<?php echo $this->Html->tag('div','oti na einai2'); ?>
<?php echo $this->MyHtml->tag('div','oti na einai'); ?>
您通过覆盖 parents 默认设置破坏了它。
protected $_defaultConfig = [];
例如,在构造函数中合并您需要的任何内容,而不是用空数组覆盖整个 属性。
你好,我正在尝试实现一个扩展 Html 助手的助手。 我根据文档创建了以下空的自定义助手
<?php
namespace App\View\Helper;
use Cake\View\Helper;
use Cake\View\Helper\HtmlHelper;
use Cake\View\View;
/**
* MyHtml helper
*/
class MyHtmlHelper extends HtmlHelper
{
public function __construct(View $view, $config = []) {
parent::__construct($view, $config);
}
/**
* Default configuration.
*
* @var array
*/
protected $_defaultConfig = [];
}
我将它们都加载到 AppController 上,但是当我在视图中使用它们时,我从 Html 助手而不是自定义助手收到了预期结果,为什么? (第二个助手没有 return 一个错误,只是一个空结果)
<?php echo $this->Html->tag('div','oti na einai2'); ?>
<?php echo $this->MyHtml->tag('div','oti na einai'); ?>
您通过覆盖 parents 默认设置破坏了它。
protected $_defaultConfig = [];
例如,在构造函数中合并您需要的任何内容,而不是用空数组覆盖整个 属性。