CodeIgniter:实现外部对象(不是模型、视图或控制器的对象)的最佳方法?

发布于 2024-08-24 09:09:07 字数 184 浏览 4 评论 0原文

我想向 CodeIgniter 项目添加一些不是模型、视图或控制器的类。它们很可能是内置模型/控制器并以各种方式使用。我应该在哪里存储这些类,在 CodeIgniter 中实现它们的最佳方法是什么? (或者更一般地说,任何基于 MVC 的框架?)。

编辑:浏览一些 codeIgniter 文档,看起来将对象添加到助手是正确的方法,对吗?

I want to add some classes to a CodeIgniter project that aren't models, views, or controllers. They will most likely be a built in Models/Controllers and used in various ways. Where should I store these classes, and what is the best way to implement them in CodeIgniter? (Or more generally, any MVC-based framework?).

EDIT: Looking through some of the codeIgniter documentation, it looks like adding the objects to a helper is the way to go, is that correct?

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

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

发布评论

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

评论(3

只想待在家 2024-08-31 09:09:07

CI 与许多 MVC 框架不同,它不会使集成外部库变得困难。一般来说,有几种方法:

  1. 只需 include()/require() 库并正常使用它。这在大多数情况下都可以正常工作,除非存在命名冲突。 CI 基本上不会造成影响,因此这种情况并不常见。在这种情况下,您可以将库放在您喜欢的任何地方。一般来说,我尝试将它们保存在文档根目录之外的共享“libs”目录中,因为我不认为它们只会由 CI 使用。

  2. 将您的库放入应用程序/库中,并使用 CI->load() 方法加载它们。这对命名约定施加了一些限制,因此您可能需要修改现有库或编写某种包装构造函数。 有关创建库的 CI 文档很好地涵盖了这一点。

就我个人而言,我更喜欢第一种方法。这是使用 CI 的好处之一。

CI is unlike a lot of MVC frameworks in that it doesn't make it challenging to integrate outside libraries. Generally, there are a couple approaches:

  1. Just include()/require() the library and use it normally. This works fine most of the time, unless there's a naming conflict. CI mostly stays out of the way, so this isn't common. In this case, you can put the library wherever you feel like. Generally I try to keep them in a shared "libs" directory, outside of the document root, since I don't assume they'll only be used by CI.

  2. Put your libraries in your applications/libraries and load them using the CI->load() method. This puts some restrictions on naming conventions, so you might need to massage existing libraries or write some sort of wrapper constructor. The CI docs on creating libraries covers this pretty well.

Personally, I much prefer the first approach. It's one of the benefits of using CI.

音栖息无 2024-08-31 09:09:07

就我个人而言,如果它不可能是一个库,我会将其作为objects_helper.php 放在helpers 文件夹中,或者在特殊情况下,放在与调用它的Library 对象相同的文件中。

您还可以查看对象工厂库,以便可以使用类似的内容:

$this->factory->createUserObject( $name, $rank, $serial );

最后,您可以查看钩子框架,但听起来您真的不想使用它。

如果您认为 require/include 在任何其他情况下没有意义(从代码/哲学角度来看),我只会使用 require/include 。尽可能在框架内工作通常是一个好主意。

Personally, if there is no possibility of it being a library, I will either put it in the helpers folder as objects_helper.php or, in special circumstances, in the same file as the Library object which calls it.

You might also look into a object factory library so you could use something like:

$this->factory->createUserObject( $name, $rank, $serial );

Finally, you could look into the hooks framework, but it does not sound like you really want to use that.

I would only use require/include if you feel that it does not make sense (from a code/philosophical perspective) in any other circumstance. It is generally a good idea to work within a framework whenever possible.

悍妇囚夫 2024-08-31 09:09:07

我建议将其作为一个库并仅使用 $this->load->library();要了解更多信息,请参阅:http://codeigniter.com/user_guide/general/creating_libraries.html

库和助手之间是有区别的。如果外部库不是面向对象的,那么您将需要它作为帮助程序,如果是,那么您应该能够毫无问题地使其成为一个库。

I would recommend making it a library and just using $this->load->library(); To read mores see: http://codeigniter.com/user_guide/general/creating_libraries.html

There is a difference between libraries and helpers. If the outside library isn't object-oriented then you'll want it as a helper, if it is, then you should just be able to make it a library with no problem.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文