CodeIgniter 和 HMVC 问题
首先,对于这篇文章给您带来的任何便利表示歉意,因为这是我第一次在这里发布问题,我需要更多时间来适应。
Q1。我想为 FrontEnd 和 BackEnd 创建 2 个“主控制器”,如下所示:
- MY_Controller 扩展 CI_Controller
- FrontEnd 扩展 MY_Controller,所有前端控制器都将扩展 FrontEnd。
- BackEnd 扩展 MY_Controller,所有后端控制器都将扩展 BackEnd。
使用 HMVC (MX) 做到这一点的最佳方法是什么?
感谢 @Wesley Murch 提出将 3 个类 MY_Controller、Frontend、Backend 放入 MY_Controller.php 的想法,但我认为将每个类放入一个 php 中文件更好(更干净)。还是我错了?我正在考虑创建这样的结构:
- ./core/MY_Controller.php (extends MX_Controller)
- ./libraries/Backend.php (extends MY_Controller)
- ./libraries/Frontend.php (extends MY_Controller)
- Auto load Backend< autoload.php 中的 /strong> 和 Frontend
- 所有前端控制器都会扩展Frontend(例如:
class Blog extends Frontend
) - 所有后端控制器都将扩展Backend(例如:
class Admin extends Backend
)
无需在后端/前端控制器中添加一行代码到 include_once 或 require_once 即可: ./libraries/Backend.php 或./libraries/Backend.php?
Q2。如何用HMVC实现多主题? 例如,在 MVC 中,我们可以有 2 个主题,如下所示:
- ./application/
- views/theme1/view_files.php ./application/views/theme2/view_files。 但是在HMVC
中,视图文件夹位于单独的文件夹内,如果我想实现多个主题,通常我必须这样做:
- ./application/modules/模块1/views/
- 主题1/view_files.php ./application/modules/模块 1/views/主题2 /view_files.php
- ./application/modules/module2/views/theme1/view_files.php
- ./application/modules/module2/views/theme2/view_files.php
这不是我想要的,因为我想将主题的所有视图文件仅放入一个文件夹中,并且稍后,如果我想创建一个新主题,我只需要复制一个主题文件夹即可。但我想知道如何在不破坏 HMVC 模型的情况下做到这一点(因为据我所知,在 HMVC 模型中,模型、视图、控制器必须位于一个模块文件夹中 - 至少对于 CI 而言)。这就是我陷入的冲突。
First of all, sorry for any convenience caused by this post because this is the first time I post a question here and I need more time to get used to with this.
Q1. I want to create 2 "master controllers" for FrontEnd and BackEnd like this:
- MY_Controller extends CI_Controller
- FrontEnd extends MY_Controller and all frontend controllers will extend FrontEnd.
- BackEnd extends MY_Controller and all backend controllers will extend BackEnd.
What's the best way to do that with HMVC (MX)?
Thanks @Wesley Murch for giving the idea to put 3 classes MY_Controller, Frontend, Backend into MY_Controller.php but I think putting each class in one php file is better (cleaner). Or am I wrong? I was thinking of creating a structure like this:
- ./core/MY_Controller.php (extends MX_Controller)
- ./libraries/Backend.php (extends MY_Controller)
- ./libraries/Frontend.php (extends MY_Controller)
- Auto load Backend and Frontend in autoload.php
- All frontend controllers will extend Frontend (E.g:
class Blog extends Frontend
) - All backend controllers will extend Backend (E.g:
class Admin extends Backend
)
Will that work without putting one more line of code in backend/frontend controllers to include_once or require_once: ./libraries/Backend.php or ./libraries/Backend.php?
Q2. How to implement multiple themes with HMVC?
For example, in MVC, we can have 2 themes strutured like this:
- ./application/views/theme1/view_files.php
- ./application/views/theme2/view_files.php
But in HMVC, views folders are inside separated folders and if I want to implement multiple themes, normally I have to do like this:
- ./application/modules/module1/views/theme1/view_files.php
- ./application/modules/module1/views/theme2/view_files.php
- ./application/modules/module2/views/theme1/view_files.php
- ./application/modules/module2/views/theme2/view_files.php
That's not what I want because I want to put all views file of a theme into only one folder and later, if I want to create a new theme, I will need to duplicate one theme folder only. But I am wondering how I can do that without breaking HMVC models (because as far as I know, in HMVC model, Models, Views, Controllers must be in one module folder - at least with CI). That is the conflict I am getting stuck at.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
只需打开或创建
core/MY_Controller.php
,创建一个MY_Controller
类并让它扩展MX_Controller
,然后在同一个文件中创建您的其他基础控制器并让它们扩展MY_Controller
。这是一个您可以复制/粘贴以开始使用的示例:就“多个主题”而言,不确定您需要什么。样式表? HTML 模板?您需要让用户切换它们还是手动进行?您是否需要检测移动设备并相应地更改主题?上述所有的? “最好”的方法将取决于您的实施。
不知道为什么你需要或想要......只是不要这样做。您应该只使用其他控制器来扩展这些类。
由于这是一个太宽泛的问题,无法在这里解决,而且您似乎还没有尝试过任何东西,所以我会给您一个最低限度 示例:
从您的控制器:
使用 HMVC,将始终在当前模块中查找该文件,如果不存在则回退到默认应用程序目录。如果出于某种原因您需要明确,您可以说在路径前面添加模块名称(例如模块之间的交叉加载资源)。示例:
无论如何,这不是一个完整的解决方案,但希望它能让您开始有了一个想法。有数百万种方法可以做到这一点,这里有两个已经支持主题的模板库:
Just open or create
core/MY_Controller.php
, create aMY_Controller
class and have it extendMX_Controller
, then in the same file create your other base controllers and have them extendMY_Controller
. Here's an example you can copy/paste to get you started:As far as "multiple themes" go, not sure what you need. Stylesheets? HTML Templates? Do you need to have the users switch them or will you do it manually? Do you need to detect mobile devices and change the theme accordingly? All of the above? The "best" way is going to depend on your implementation.
Not sure why you'd need or want to... just don't do it. You should only extend these classes with other controllers.
Since this is too broad a question to tackle here, and you don't seem to have even tried anything yet, I'll will give you a bare minimum example:
From your controller:
Using HMVC, the file will always be looked for in the current module, then fall back to the default application directories if it doesn't exist. If for some reason you need to be explicit, you can say prepend the path with the module name (like for cross-loading resources between modules). Example:
Anyways, this is not a full solution, but hopefully it gets you started with an idea. There are millions of ways to do this, here are two template libraries that already support themes: