CodeIgniter 和 HMVC 问题 [英] CodeIgniter and HMVC questions

查看:28
本文介绍了CodeIgniter 和 HMVC 问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

首先,很抱歉这篇帖子给您带来的任何便利,因为这是我第一次在这里发布问题,我需要更多时间来适应这个问题.

第一季度.我想为 FrontEndBackEnd 创建 2 个主控制器",如下所示:

Q1. I want to create 2 "master controllers" for FrontEnd and BackEnd like this:

  • MY_Controller 扩展了 CI_Controller
  • FrontEnd 扩展 MY_Controller,所有前端控制器都将扩展 FrontEnd.
  • BackEnd 扩展 MY_Controller,所有后端控制器都将扩展 BackEnd.
  • 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.

使用 HMVC (MX) 的最佳方法是什么?

感谢@Wesley Murch 提出将 3 个类 MY_Controller、Frontend、Backend 放入 MY_Controller.php 的想法,但我认为将每个类放在一个 php 文件中更好(更干净).还是我错了?我正在考虑创建一个这样的结构:

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(扩展 MX_Controller)
  • ./libraries/Backend.php(扩展 MY_Controller)
  • ./libraries/Frontend.php(扩展 MY_Controller)
  • autoload.php
  • 中自动加载BackendFrontend
  • 所有前端控制器都将扩展 Frontend(例如:class Blog extends Frontend)
  • 所有后端控制器都将扩展 Backend(例如:class Admin extends Backend)
  • ./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)

如果不将后端/前端控制器中的多行代码添加到 include_oncerequire_once 中,这是否可行:./libraries/Backend.php./libraries/Backend.php?

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?

第二季度.如何使用 HMVC 实现多个主题?例如,在 MVC 中,我们可以有 2 个主题结构如下:

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
  • ./application/views/theme1/view_files.php
  • ./application/views/theme2/view_files.php

但在 HMVC 中,视图文件夹位于单独的文件夹中,如果我想实现多个主题,通常我必须这样做:

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
  • ./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

这不是我想要的,因为我想将一个主题的所有视图文件放在一个文件夹中,然后,如果我想创建一个新主题,我只需要复制一个主题文件夹.但我想知道如何在不破坏 HMVC 模型的情况下做到这一点(因为据我所知,在 HMVC 模型中,模型、视图、控制器必须位于一个模块文件夹中 - 至少使用 CI).这就是我陷入的冲突.

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.

推荐答案

只要打开或创建core/MY_Controller.php,创建一个MY_Controller类并让它扩展MX_Controller,然后在同一个文件中创建您的其他基本控制器并让它们扩展 MY_Controller.这是一个您可以复制/粘贴以帮助您入门的示例:

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 */

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

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.

我正在考虑创建 2 个从 MY_Controller.php 扩展的库并自动加载它们.那会奏效吗?

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.

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

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/
    }

}

从您的控制器:

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

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

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:

这篇关于CodeIgniter 和 HMVC 问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆