一个网站有两个应用程序文件夹:如何在 CodeIgniter 中共享代码?
我有一个 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
好吧,你有几个选择:
让事情保持原样,不用担心“违反规则”。您很可能会发现,最终您的管理/控制面板将需要一个与前端截然不同的代码库 - 因此,当您最终编写不同的代码时,重复就不再是问题了。
将两个应用程序集成为一个。这解决了重复问题,但会导致其他问题,例如需要平衡每个库或帮助程序以在两种环境中正常工作。我的经验在此处有更详细的介绍,并且CI 开发人员 Phil Sturgeon 发表了一篇博客文章,总结了如何在此处实施此实践:http://philsturgeon.co.uk/blog/ 2009/07/Create-an-Admin-panel-with-CodeIgniter
这是一个我没有使用过但看起来很有前途的:软件包。
无论建议如何使用
/third_party
目录,此可以是您选择的任何目录。完整的详细信息在加载器的文档中进行了解释:http://codeigniter.com/user_guide/libraries/loader.html
再一次,这是 Phil Sturgeon 关于它的另一篇文章这可能会帮助您了解它是如何工作的: http://philsturgeon.co .uk/blog/2010/04/codeigniter-packages-modules
这里的想法是,您将有一个第三“应用程序”文件夹(您的“包”),其中将用作加载资源的后备,同时允许您通过在其他应用程序中放置匹配的文件来覆盖文件,因此您可以灵活地覆盖它们*,从而获得共享资源的好处。
* 这是我在阅读文档时做出的假设,我尚未对此进行测试,因此可能还有更多内容。
您可能希望将包目录放在其他两个应用程序文件夹旁边,并在 MY_Controller::__construct() 中引用它,例如:
我自己还没有使用过包,但是在写完这篇文章之后 - 我很想在我的下一个项目中检查它。
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.
Regardless of the recommendation to use the
/third_party
directory, this can be any directory you choose.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:I haven't used yet packages myself, but after writing this post - I'm very tempted to check it out for my next project.