CodeIgniter 和 HMVC 问题

发布于 2024-11-25 18:31:20 字数 2166 浏览 1 评论 0原文

首先,对于这篇文章给您带来的任何便利表示歉意,因为这是我第一次在这里发布问题,我需要更多时间来适应。

Q1。我想为 FrontEndBackEnd 创建 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_oncerequire_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 技术交流群。

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

发布评论

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

评论(1

过气美图社 2024-12-02 18:31:20

只需打开或创建 core/MY_Controller.php,创建一个 MY_Controller 类并让它扩展 MX_Controller,然后在同一个文件中创建您的其他基础控制器并让它们扩展MY_Controller。这是一个您可以复制/粘贴以开始使用的示例:

<?php defined('BASEPATH') OR exit('No direct script access.');

class MY_Controller extends MX_Controller {

    public function __construct()
    {
        // do some stuff here that affects all controllers
    }

}

class Frontend_Controller extends MY_Controller {

    public function __construct()
    {
        parent::__construct();
    }

}

class Backend_Controller extends MY_Controller {

    public function __construct()
    {
        parent::__construct();
        // Check admin login, etc.
    }

}

/* end file application/core/MY_Controller.php */

就“多个主题”而言,不确定您需要什么。样式表? HTML 模板?您需要让用户切换它们还是手动进行?您是否需要检测移动设备并相应地更改主题?上述所有的? “最好”的方法将取决于您的实施。

我正在考虑创建 2 个从 MY_Controller.php 扩展的库
并自动加载它们。那行得通吗?

不知道为什么你需要或想要......只是不要这样做。您应该只使用其他控制器来扩展这些类。

关于主题,我想要
视图的多个主题,例如: - /views/theme1/view_files.php -
/views/theme2/view_files.php 关于js/css/images,我可以安排
我。一开始我会确定主题,但稍后我可能会允许
用户来选择。使用 MVC,我可以将主题放在 /views/ 的子​​文件夹中,如下所示
上面但是对于 HMVC,我必须找到另一种方法来将它们排列到
主题,因为视图文件夹是分开的(我想要的所有视图文件
相同的主题将仅位于 1 个文件夹中)..

由于这是一个太宽泛的问题,无法在这里解决,而且您似乎还没有尝试过任何东西,所以我会给您一个最低限度 示例:

class MY_Controller extends MX_Controller {

    public function __construct()
    {
        // do some stuff here that affects all controllers
        $this->theme = 'theme1'; // matches your directory name in /views/themes/
    }

}

从您的控制器:

$this->load->view('themes/'.$this->theme.'/my_view_file');

使用 HMVC,将始终在当前模块中查找该文件,如果不存在则回退到默认应用程序目录。如果出于某种原因您需要明确,您可以说在路径前面添加模块名称(例如模块之间的交叉加载资源)。示例:

// From "blog" module
$this->load->view('events/index');
// We just loaded `modules/events/views/index` from the blog module

无论如何,这不是一个完整的解决方案,但希望它能让您开始有了一个想法。有数百万种方法可以做到这一点,这里有两个已经支持主题的模板库:

Just open or create core/MY_Controller.php, create a MY_Controller class and have it extend MX_Controller, then in the same file create your other base controllers and have them extend MY_Controller. Here's an example you can copy/paste to get you started:

<?php defined('BASEPATH') OR exit('No direct script access.');

class MY_Controller extends MX_Controller {

    public function __construct()
    {
        // do some stuff here that affects all controllers
    }

}

class Frontend_Controller extends MY_Controller {

    public function __construct()
    {
        parent::__construct();
    }

}

class Backend_Controller extends MY_Controller {

    public function __construct()
    {
        parent::__construct();
        // Check admin login, etc.
    }

}

/* end file application/core/MY_Controller.php */

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.

I am thinking of creating 2 libraries extends from MY_Controller.php
and auto load them. Will that work?

Not sure why you'd need or want to... just don't do it. You should only extend these classes with other controllers.

About the themes, I want to have
multiple themes for views like: - /views/theme1/view_files.php -
/views/theme2/view_files.php About js/css/images, I can arrange
myself. At the beginning I will fix the theme but later, I may allow
user to choose. With MVC, I can put themes in subfolders of /views/ as
above but with HMVC, I have to find another way to arrange them in to
themes because view folders are separated (I want all view files of
same theme will be in only 1 folder)..

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:

class MY_Controller extends MX_Controller {

    public function __construct()
    {
        // do some stuff here that affects all controllers
        $this->theme = 'theme1'; // matches your directory name in /views/themes/
    }

}

From your controller:

$this->load->view('themes/'.$this->theme.'/my_view_file');

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:

// From "blog" module
$this->load->view('events/index');
// We just loaded `modules/events/views/index` from the blog module

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:

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