CodeIgniter:创建库&帮助在CodeIgniter [英] CodeIgniter: Decision making for creating of library & helper in CodeIgniter

查看:118
本文介绍了CodeIgniter:创建库&帮助在CodeIgniter的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在CodeIgniter中开发了一段时间后,我发现很难做出决定何时创建自定义库,以及何时创建自定义助手。

After developing in CodeIgniter for awhile, I find it difficult to make decisions when to create a custom library and when to create a custom helper.

我明白它们都允许在其中具有业务逻辑,并且可以在整个框架中重用(从不同的控制器等调用)。

I do understand that both allow having business logic in it and are reusable across the framework (calling from different controller etc.)

但我坚信CI核心开发人员从帮助者,必须有一个背后的原因,我想,这是等待我发现和开明的原因。

But I strongly believe that the fact that CI core developers are separating libraries from helpers, there has to be a reason behind it and I guess, this is the reason waiting for me to discover and get enlightened.

CI开发人员在那里,pls建议

CI developers out there, pls advise.

我认为最好包含一个例子。

i think it's better to include an example.


$ b

I could have a

class notification_lib {
   function set_message() { /*...*/}

   function get_message() {/*...*/}

   function update_message() {/*...*/}
}

或者,我也可以将所有函数包含在助手中。

Alternatively, i could also include all the functions into a helper.

notification_helper.php 文件,我将包括 set_message(),get_message(),update_message() ..

In a notification_helper.php file, i will include set_message(), get_message(), update_message()..

无论哪种方式,它仍然可以重用。所以这让我想起了关于我们什么时候创建一个库和一个帮助器,特别是在CI中的决策点。

Where either way, it still can be reused. So this got me thinking about the decision making point about when exactly do we create a library and a helper particularly in CI.

在一个正常(无框架)php应用程序,选择很清楚,因为没有助手,你只需要创建一个库以便重用代码。但在这里,在CI中,我想了解图书馆和帮助者的核心开发者分隔。

In a normal (framework-less) php app, the choice is clear as there is no helper, you will just need to create a library in order to reuse codes. But here, in CI, I would like to understand the core developers seperation of libraries and helpers

推荐答案

我自己在决定这个,我认为也会帮助你。问题是:我是否为我的框架提供了一个功能,或者我正在整合?

There is a question I ask myself when deciding this that I think will help you as well. The question is: Am I providing a feature to my framework or am I consolidating?

如果你有一个要添加到框架中的功能,创建一个库。例如,表单验证是您添加到框架的一个功能。即使您可以在没有此库的情况下进行表单验证,也可以创建一个用于验证的标准系统,这是一个功能。

If you have a feature that you are adding to your framework, then you'll want to create a library for that. Form validation, for example, is a feature that you are adding to a framework. Even though you can do form validation without this library, you're creating a standard system for validation which is a feature.

但是,还有一个窗体助手您创建表单的HTML。与表单验证库的巨大区别在于,表单助手不会创建新的功能,它只是一组相关的函数,可以帮助您正确地编写表单的HTML。

However, there is also a form helper which helps you create the HTML of forms. The big difference from the form validation library is that the form helper isn't creating a new feature, its just a set of related functions that help you write the HTML of forms properly.

希望这种差异化会帮助你,因为它有我。

Hopefully this differentiation will help you as it has me.

这篇关于CodeIgniter:创建库&帮助在CodeIgniter的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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