如何使用通用函数在帮助和组件在Cakephp [英] how to use common function in helper and component In Cakephp

查看:154
本文介绍了如何使用通用函数在帮助和组件在Cakephp的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我们熟悉CakePHP中的组件和助手。

We are familiar with Components and Helpers in CakePHP.

我有一个ABC组件和XYZ助手,它们都有相同的功能(大约2000行总共4000行)。
有任何方法在控制器和.CTP文件中使用相同的功能。使用相同的函数2时间不好。

I have an ABC Component and XYZ helper and both have same function (around 2000 lines total 4000 lines). there is any way to use same function in Controller and .CTP files. it's not good to use same function 2 time.

任何方法,所以我可以使用Helper / Component中的Component / Helper函数而不重写?

any method so i can use Component/Helper function in Helper/Component without rewrite ?

在组件和帮助器中使用相同的方法>>

same method into component and helper >>

组件

class DATAComponent extends Component

class DATAComponent extends Component {

public $components = array('Session', 'THmail');

public function UsaStateList()
{ /********/}

助手

class LabHelper extends AppHelper {
    public function UsaStateList()
    { /********/}
}


推荐答案

好吧,你必须重写某事,它不会自己解决。

Well, you will have to rewrite something, it's not going to solve itself.

CakePHP仍然是PHP,所以你可以很容易地应用通用模式来保持干燥。最直接的方式可能是将共享功能移动到一个实用程序类中,您的组件和帮助程序可以在内部使用,而保持其公共API不变。

CakePHP is still PHP, so you can easily apply common patterns to keeps things DRY. The most straight forward way would probably be to move the shared functionality into an utility class that your component and helper can both use internally while leaving their public API unchanged.

CakePHPs帮助者做类似的事情,检查例如时间助手。

Some of CakePHPs helpers do something similar, check for example the time helper.

  • http://book.cakephp.org/2.0/en/core-libraries/helpers/time.html
  • http://book.cakephp.org/2.0/en/core-utility-libraries/time.html#CakeTime

性状 也可能是一个选项,具体取决于共享的功能量以及与其相关的数量在组件/帮助中的使用。

Traits might be an option too, depending on the amount of functionality being shared and how much it is tied to the use in a component/helper.

这篇关于如何使用通用函数在帮助和组件在Cakephp的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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