如何在 cakephp 3 中使用别名助手?

How to use Aliasing Helpers in cakephp 3?

我需要修改 Html->link 以在生成 link 之前检查 acl。然后我使用别名助手来做到这一点。我在 appController

public $helpers = ['Tools' , 'Html' => ['className' => 'Mhtml']];

在 src/View/Helper/MhtmlHelper.php 我有

<?php
namespace App\View\Helper;
use Cake\View\Helper;
use Cake\View\Helper\HtmlHelper;

class MhtmlHelper extends HtmlHelper {

    public function acl() {
        //return true if it is able to verify the user’s access, else false
    }

    public function link($title , $url=null , $options=[]) {
        return $this->acl ? parent::link($title , $url , $options) : '';
    }

}

但是我运行进入这个错误

Strict (2048): Declaration of App\View\Helper\MhtmlHelper::link() should be compatible with Cake\View\Helper\HtmlHelper::link($title, $url = NULL, array $options = Array) [APP/View\Helper\MhtmlHelper.php, line 6]

怎么了?

您需要像这样声明 link 函数:

public function link($title , $url = null , array $options = []) {
    return $this->acl ? parent::link($title , $url , $options) : '';
}