Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

CodeIgniter and HMVC questions

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.

like image 927
Michael Bui Avatar asked Jul 22 '11 09:07

Michael Bui


1 Answers

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:

  • http://codeigniter.com/wiki/Phil_Sturgeon%27s_Template_Library/
  • http://williamsconcepts.com/ci/codeigniter/libraries/template/
like image 96
Wesley Murch Avatar answered Nov 13 '22 05:11

Wesley Murch