CodeIgniter and HMVC questions
Asked Answered
M

1

7

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.

Mcarthur answered 22/7, 2011 at 9:53 Comment(0)
S
9

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:

Shirlshirlee answered 22/7, 2011 at 13:40 Comment(6)
I am thinking of creating 2 libraries extends from MY_Controller.php and auto load them. Will that work? 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)..Mcarthur
Sorry, I forgot to mention instead of creating 2 classes in 1 files, I want to create 2 libraries Backend and Frontend extends from MY_Controller and autoload Backend and Frontend in config files. Then in controllers, I will extend like: class Blog extends FrontendMcarthur
About the themes, because in HMVC model, views folders are in separated module folders and that's reason I got stuck in grouping them into 1 theme folder. With your suggestion, is theme1 inside /application/views/ or in /application/modules/module1/views/? If it's inside /application/views/, them HMVC model will be broken, right? But if it's inside /application/modules/module1/views/ then how to group all view files of a theme into 1 folder (then later if I want to create a new theme, I just need to duplicate that folder)?Mcarthur
That's highly unusual. What is wrong with the (common) method I posted? Also, please clarify the question by editing it, not posting comments. For instance, I could delete my answer and no one would see your updates. Also, you're asking two very unrelated questions here, it would be better to make them two separate ones.Shirlshirlee
I am sorry, I clicked on upvote arrow but it's not allowed because I have only 6 points. I have just updated my post as your advice. Thank your very much for helping me. I definitely find your answer is very helpful to me on clarifying creating Backend and Frontend master controllers. The only thing I am getting stuck at is organizing view files into one theme folder without breaking the concept of HMVC models (each MVC triagle must be isolated as a small system).Mcarthur
My advice on the themes: Spend some time working on it, and when you get stuck - open a new question here, and give specific details. Cramming two unrelated questions into one is a bad idea.Shirlshirlee

© 2022 - 2024 — McMap. All rights reserved.