Cakephp 覆盖 HtmlHelper::link

发布于 2024-10-31 21:03:23 字数 139 浏览 1 评论 0原文

我想设置 HtmlHelper::link() 方法,以便默认选项数组具有 escape = false。

如何在不改变核心类的情况下实现这一目标?

OBS:我已经清理了表单输入,所以我想这不会有问题。

提前致谢。

I want to setup HtmlHelper::link() method so the default options array have escape = false.

How can I achieve this without changing the core class?

OBS: I already sanitized form input, so I guess this will have no problem.

Thanks in advance.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(7

人海汹涌 2024-11-07 21:03:24

Cake 2.1.5

我刚刚实现了这个,我想指出一些事情:

您的自定义 html 帮助程序应该扩展 HTML 帮助程序(并且不要忘记包含 HTML 帮助程序类)

App::uses('HtmlHelper', 'View/Helper');
class CustomHtmlHelper extends HtmlHelper {
   //yadda yadda
}

此外,您的调用AppController 中不应包含 Helper 一词:

'Html'=> array('className' =>'CustomHtml'),

Cake 2.1.5

I just implemented this and I wanted to point out a few things:

Your custom html helper should extend HTML helper (and don't forget to include the HTML helper class)

App::uses('HtmlHelper', 'View/Helper');
class CustomHtmlHelper extends HtmlHelper {
   //yadda yadda
}

Additionally, your call in AppController should not include the word Helper:

'Html'=> array('className' =>'CustomHtml'),
丢了幸福的猪 2024-11-07 21:03:24

在 Cake 2.0

创建包含链接方法的 OwnHelper 类,该方法扩展 HtmlHelper,在 AppController 中指定:

$helpers = array('Html' => array('className' => 'OwnHelper'));

通过 ADmad

In Cake 2.0

Create your OwnHelper class containing a link method, which extends HtmlHelper, in AppController specify:

$helpers = array('Html' => array('className' => 'OwnHelper'));

via ADmad

东京女 2024-11-07 21:03:24

为什么不创建自己的自定义帮助器并创建一个返回带有选项集的 HTMLHelper 链接的方法?

http://book.cakephp.org/view/102/Inclusion-other-Helpers

class MyHelper extends AppHelper {
  var $helpers = array('html');

  function linkNoEscape($title, $url)
    $options = array(); //set custom options, e.g. no escape 

    return $this->Html->link($title, $url, $options);
  }
}

Why don't you create your own custom helper and create a method that returns the HTMLHelper's link with the options set?

http://book.cakephp.org/view/102/Including-other-Helpers

class MyHelper extends AppHelper {
  var $helpers = array('html');

  function linkNoEscape($title, $url)
    $options = array(); //set custom options, e.g. no escape 

    return $this->Html->link($title, $url, $options);
  }
}
谁与争疯 2024-11-07 21:03:24

我从来不喜欢重写层次结构中较高的方法(即在 AppHelper 中),因为您总是很有可能破坏其他依赖的帮助程序。

希望能够尽快发表评论,而不是给出垃圾一半的答案!

同样相关的是:我听说 CakePHP 2.0 将允许助手、组件等使​​用别名。例如,您想要更改 HtmlHelper 的输出,您可以将其替换为您自己的版本,而无需更改所有视图模板。

I'm never comfortable overriding methods higher up in the hierarchy (ie in AppHelper) because there is always a good chance you break other helpers that are dependent.

Hoping to be able to comment soon, instead of giving rubbish half answers!

Also relevant: I heard that CakePHP 2.0 will allow helpers, components etc to be aliased. Eg you want to change the output from HtmlHelper, you can replace it with your own version without changing all your view templates.

嘦怹 2024-11-07 21:03:24

您可以将原始的 HTMLHelper 从 Cake/libs/view/helpers 复制到 app/views/helpers 并修改 link() 方法在那里。

You could copy the original HTMLHelper from cake/libs/view/helpers to app/views/helpers and modify the link() method there.

抹茶夏天i‖ 2024-11-07 21:03:24

我认为 cakephp 应该实现并且您也可以实现的另一个好习惯是一个简单的工厂模式助手。以下内容应仅视为伪代码,而不是真正的代码。

$this->Factory->getHelper('Html')->link();

而不是

$this->Html->link();

采用以下示例

class FactoryHelper extends  Helper {
    public function getHelper($name) {
        if(Configure::read('Overrides.{$name}')) {
            return $this->{Configure::read('Overrides.{$name}')};
        }
        return (isset($this->{$name})?$this->{$name}:false);
    }

}

//Bootstrap is where you will set all your overrides
Configure::write('Overrides',array(
    'Html'=>'NewHtml'
));

//所以现在当您想要覆盖任何帮助器时,您可以

现在在引导程序中设置覆盖 Html Helper。在整个站点中,将调用新的“NewHtml”助手,而不是传统的助手。

another good practice that I think cakephp should implement that you also can implement is a simple Factory Pattern Helper. The following should be consider just psuedo not real code.

$this->Factory->getHelper('Html')->link();

instead of

$this->Html->link();

take the following for example

class FactoryHelper extends  Helper {
    public function getHelper($name) {
        if(Configure::read('Overrides.{$name}')) {
            return $this->{Configure::read('Overrides.{$name}')};
        }
        return (isset($this->{$name})?$this->{$name}:false);
    }

}

//Bootstrap is where you will set all your overrides
Configure::write('Overrides',array(
    'Html'=>'NewHtml'
));

//so now when you want to override any helper you can

So now in the bootstrap that you set to override Html Helper. Throughout your entire site, your new 'NewHtml' helper will be called instead of the traditional helper.

风月客 2024-11-07 21:03:24

您可以覆盖 AppHelper 中的任何帮助器方法,因此

class AppHelper extends Helper{
    function link($params, $go, $here){ ... code ...}
}

you can overwrite any helper method from AppHelper, so

class AppHelper extends Helper{
    function link($params, $go, $here){ ... code ...}
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文