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

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

问题描述

首先,对于此信息造成的任何方便,对不起,因为这是我第一次在这里发布问题,我需要更多的时间来习惯这个。



Q1。我想为 FrontEnd BackEnd 创建2个主控制器,如下所示:




  • MY_Controller 扩展 CI_Controller

  • FrontEnd 控制器将扩展 FrontEnd

  • BackEnd 扩展 MY_Controller ,所有后端控制器将扩展



使用HMVC(MX)的最佳方式是什么?感谢@Wesley Murch给的想法把3个类MY_Controller,Frontend,后端放入MY_Controller.php,但我认为将每个类放在一个php文件是更好(更清洁)。还是我错了?我正在考虑创建一个这样的结构:




  • ./ core / MY_Controller.php(extends MX_Controller)

  • ./ libraries / Backend.php(扩展MY_Controller)

  • ./ libraries / Frontend.php(扩展MY_Controller)

  • autoload.php

  • 前端所有前端控制器都会延伸前端(例如: class Blog extend Frontend

  • 所有后端控制器都会扩展后端 class Admin extends Backend



后端/前端控制器中的代码到 include_once require_once ./ libraries / Backend.php ./ libraries / Backend.php






如何用HMVC实现多个主题?
例如,在MVC中,我们可以有2个主题,如下所示:




  • ./ application / views / theme1 /view_files.php


  • ./ application / views / theme2 /view_files.php <

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




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

    解决方案

    只需打开或创建 core / MY_Controller.php ,创建一个 MY_Controller 类并让它扩展 MX_Controller 你的其他基本控制器,并让它们扩展 MY_Controller 。以下是您可以复制/粘贴以开始使用的示例:

     <?php defined('BASEPATH')OR exit '没有直接脚本访问。 

    class MY_Controller extends MX_Controller {

    public function __construct()
    {
    //在这里做一些影响所有控制器的东西
    }

    }

    class Frontend_Controller extends MY_Controller {

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

    }

    class Backend_Controller extends MY_Controller {

    public function __construct()
    {
    parent ::__构造();
    //检查管理员登录等
    }

    }

    / *结束文件应用程序/ core / MY_Controller.php * /

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


    我想创建2个库从MY_Controller.php
    并自动加载它们。会工作吗?


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


    关于主题,我想要有
    多个主题的视图: - /views/theme1/view_files.php -
    /views/theme2/view_files.php关于js / css / images,我可以自己安排
    。在开始我将修复的主题,但后来,我可以允许
    用户选择。有了MVC,我可以把主题放在/ views / as
    的子文件夹中,但是与HMVC,我必须找到另一种方式来安排他们在
    主题,因为视图文件夹是分开的(我想要所有的视图文件
    同一主题将只在一个文件夹中)..


    由于这是太宽泛的问题要解决这里,您似乎尚未尝试任何操作,我会给您一个最低限度示例:

      class MY_Controller extends MX_Controller {

    public function __construct()
    {
    //在这里做一些影响所有控制器的东西
    $ this-> ; theme ='theme1'; //与您的目录名称匹配/ views / themes /
    }

    }

    来自您的控制器:

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

    使用HMVC,文件将始终在当前模块中查找,应用程序目录(如果它不存在)。如果由于某种原因你需要显式的话,你可以用模块名称来预置路径(就像在模块之间交叉加载资源一样)。示例:

      //从博客模块
    $ this-> load-> view /指数');
    //我们刚刚从博客模块加载`modules / events / views / index`

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




    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.

    解决方案

    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:

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

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