如何扩展或覆盖CakePHP核心帮助器的功能/方法 [英] How to extend or override functionality/methods of a CakePHP Core Helper

查看:91
本文介绍了如何扩展或覆盖CakePHP核心帮助器的功能/方法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这样: Cakephp覆盖HtmlHelper :: link 提出了一个非常相似的问题,但没有完整的答案。也许现在,与蛋糕2出来,会有。

This: Cakephp Override HtmlHelper::link asks a very similar question, but there were no complete answers. Perhaps now, with Cake 2 out, there will be.

我想创建一个自定义助手是Cake的Paginator Helper的子类。我想让我的新帮手重写Cake的Paginator帮助器的'numbers'方法,但我想让它继承所有其他方法。

I want to create a custom helper that is a subclass of Cake's Paginator Helper. I want my new helper to override the 'numbers' method of Cake's Paginator helper, but I want it to inherit all other methods.

这条路?显然,我不想:修改Cake Core;将不必要的代码放在AppHelper超类中;

Is it possible to extend core helpers in this way? Obviously, I don't want to: modify the Cake Core; put unnecessary code in the AppHelper superclass; or duplicate the entire core Pagination Helper into my new helper.

推荐答案

添加到Brelsnok的解决方案上,别名是正确的方法。如果你添加这个代码到你的AppController.php文件,任何时候使用Paginator,它将使用你的扩展类。

Adding onto Brelsnok's solution, aliasing is the right way to do it. If you add this code to your AppController.php file, anytime that you use the Paginator, it will use your extended class.

public $helpers = array(
'Paginator' => array('className' => 'PaginatorExt' )
);

由于PaginatorExtHelper类已经扩展了PaginatorHelper,唯一被覆盖的函数是 numbers 。对其他Paginator方法的任何调用都将由核心PaginatorHelper类处理,就像它是一个香草Cake安装。

Since the PaginatorExtHelper class already extends PaginatorHelper, the only function being overridden is numbers. Any calls to other Paginator methods will be handled by the core PaginatorHelper class, just as if it was a vanilla Cake install.

这篇关于如何扩展或覆盖CakePHP核心帮助器的功能/方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆