如何在Cakephp中的helper和component中使用通用函数 [英] how to use common function in helper and component In Cakephp

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

问题描述

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

We are familiar with Components and Helpers in CakePHP.

我有一个 ABC Component 和 XYZ helper,它们都具有相同的功能(大约 2000 行,总共 4000 行).有任何方法可以在 Controller 和 .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 扩展组件 {

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.

Traits 也可能是一种选择,具体取决于共享的功能数量和数量与组件/助手中的使用相关联.

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中的helper和component中使用通用函数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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