一个网站有两个应用程序文件夹:如何在 CodeIgniter 中共享代码?

发布于 2024-11-30 21:22:14 字数 375 浏览 0 评论 0原文

我有一个 CI 网站,其中包含两个应用程序文件夹,一个用于前端(用户),另一个用于管理员用户:

/applications/front/
/applications/admin/

/applications/front/helpers/ 内,我们有一些类,例如 MY_url_helper.php,然后因为我们想在管理区域中使用它,所以我们在 /applications/admin/helpers/MY_url_helper.php - 所以当我们修改代码时,它在两个地方。

正如你可以想象的,这违反了很多规则!在使用 CI 的单个网站上与两个应用程序共享相同的帮助器/库的更好方法是什么?

I have a CI website which contains two application folders, one for the front end (users) and another for the admin user:

/applications/front/
/applications/admin/

Inside the /applications/front/helpers/ we have some classes like MY_url_helper.php, and then because we want to use this in the admin area, we have the same exact code in
/applications/admin/helpers/MY_url_helper.php - so when we modify the code, it's in two places.

As you can imagine, this goes against SOO many rules! What's a better way of sharing the same helpers/libraries with two applications on a single website with CI?

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

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

发布评论

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

评论(1

2024-12-07 21:22:14

好吧,你有几个选择:

  • 让事情保持原样,不用担心“违反规则”。您很可能会发现,最终您的管理/控制面板将需要一个与前端截然不同的代码库 - 因此,当您最终编写不同的代码时,重复就不再是问题了。

  • 将两个应用程序集成为一个。这解决了重复问题,但会导致其他问题,例如需要平衡每个库或帮助程序以在两种环境中正常工作。我的经验在此处有更详细的介绍,并且CI 开发人员 Phil Sturgeon 发表了一篇博客文章,总结了如何在此处实施此实践:http://philsturgeon.co.uk/blog/ 2009/07/Create-an-Admin-panel-with-CodeIgniter

  • 这是一个我没有使用过但看起来很有前途的:软件包。

应用程序“包”

应用程序包可以轻松分发完整的
单个目录中的资源集,有自己的完整资源
库、模型、助手、配置和语言文件。这是
建议将这些包放置在
application/third_party 文件夹。

无论建议如何使用 /third_party 目录,此可以是您选择的任何目录。

以下是应用程序包的目录示例
名为“Foo Bar”。 /application/third_party/foo_bar

<前>配置/
帮手/
语言/
图书馆/
型号/

无论“Foo Bar”应用程序包的目的是什么,它都有它的用途
自己的配置文件、帮助程序、语言文件、库和模型。到
在你的控制器中使用这些资源,你首先需要告诉
您将从包中加载资源的加载器,方法是
添加包路径。 $this->load->add_package_path()

添加包路径会指示 Loader 类在前面添加给定的路径
后续资源请求的路径。举个例子,“Foo
上面的“Bar”应用程序包有一个名为 Foo_bar.php 的库。在我们的
控制器,我们会执行以下操作:

$this->load->add_package_path(APPPATH.'third_party/foo_bar/');
$this->load->library('foo_bar');

完整的详细信息在加载器的文档中进行了解释:http://codeigniter.com/user_guide/libraries/loader.html

再一次,这是 Phil Sturgeon 关于它的另一篇文章这可能会帮助您了解它是如何工作的: http://philsturgeon.co .uk/blog/2010/04/codeigniter-packages-modules

这里的想法是,您将有一个第三“应用程序”文件夹(您的“包”),其中将用作加载资源的后备,同时允许您通过在其他应用程序中放置匹配的文件来覆盖文件,因此您可以灵活地覆盖它们*,从而获得共享资源的好处。

* 这是我在阅读文档时做出的假设,我尚未对此进行测试,因此可能还有更多内容。

您可能希望将包目录放在其他两个应用程序文件夹旁边,并在 MY_Controller::__construct() 中引用它,例如:

$this->load->add_package_path(FCPATH.'my_package_name/');

我自己还没有使用过包,但是在写完这篇文章之后 - 我很想在我的下一个项目中检查它。

Well, you have a few options:

  • Leave things the way they are and don't worry about "breaking the rules". You will most likely find that, eventually, you will need a very different codebase for your admin/control panel than for your front end - so duplication becomes a non-issue as you end up writing different code.

  • Integrate both applications into one. This solves the duplication issue but causes others, like the need to balance each library or helper to work appropriately in both environments. My experiences with this are exposed in greater detail here, and there is a blog post by CI developer Phil Sturgeon that sums up how to implement this practice here: http://philsturgeon.co.uk/blog/2009/07/Create-an-Admin-panel-with-CodeIgniter

  • This is one I haven't used but looks promising: Packages.

Application "Packages"

An application package allows for the easy distribution of complete
sets of resources in a single directory, complete with its own
libraries, models, helpers, config, and language files. It is
recommended that these packages be placed in the
application/third_party folder.

Regardless of the recommendation to use the /third_party directory, this can be any directory you choose.

The following is an example of a directory for an application package
named "Foo Bar". /application/third_party/foo_bar

config/
helpers/
language/
libraries/
models/

Whatever the purpose of the "Foo Bar" application package, it has its
own config files, helpers, language files, libraries, and models. To
use these resources in your controllers, you first need to tell the
Loader that you are going to be loading resources from a package, by
adding the package path. $this->load->add_package_path()

Adding a package path instructs the Loader class to prepend a given
path for subsequent requests for resources. As an example, the "Foo
Bar" application package above has a library named Foo_bar.php. In our
controller, we'd do the following:

$this->load->add_package_path(APPPATH.'third_party/foo_bar/');
$this->load->library('foo_bar');

The full details are explained on the docs for the Loader: http://codeigniter.com/user_guide/libraries/loader.html

And once again, here is another post by Phil Sturgeon about it that may help you understand how it works: http://philsturgeon.co.uk/blog/2010/04/codeigniter-packages-modules

The idea here is that you would have a third "application" folder (your "package") which would be used as a fallback for loading resources, while allowing you to override the file by placing a matching one in either of the other applications, so you get the benefit of shared resources with the flexibility of being able to override them *.

* This is the assumption I am under from reading the docs, I have not tested this so there may be more to it.

You would probably want to place the package directory next to your other two application folders, and reference it in MY_Controller::__construct() like:

$this->load->add_package_path(FCPATH.'my_package_name/');

I haven't used yet packages myself, but after writing this post - I'm very tempted to check it out for my next project.

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