Git Product home page Git Product logo

codeigniter-hmvc-modules's Introduction

CodeIgniter HMVC Modules

This extension for CodeIgniter enables the use of the Hierarchical Model View Controller(HMVC) pattern and makes your application modular. This allows easy distribution of independent components (MVC) in a single directory across other CodeIgniter applications. All modules are grouped in their own folder and can have their own controller, model, view, library, config, helper and language files.

Installation

Download the file from github and place them into their corresponding folders in the application directory.

Note: to use the HMVC functionality, make sure the Controller and Router you use extend their HMVC class.

Next, add the location of your modules directory to the main config.php file:

/*
|--------------------------------------------------------------------------
| Modules locations
|--------------------------------------------------------------------------
|
| These are the folders where your modules are located. You may define an
| absolute path to the location or a relative path starting from the root
| directory.
|
*/

$config['modules_locations'] = array(APPPATH . 'modules/');

Functionallity

This is the basic structure of a HMVC module:

/modules
    /module
       /controllers
       /config
       /helpers
       /language
       /libraries
       /models

From within a module you can load its own resources just like you always do. If on the other hand, you want to load resources from another module you can do this by adding the module's name like a directory structure:

class Hello extends CI_Controller {
    
    public function index() {
        // load a model from the current module
        $this->load->model('local_model');
        
        // load a model from another module
        $this->load->model('other_module/model');

        // HMVC example
        $this->load->controller('module/controller/method', $params = array(), $return = FALSE);
    }
}

Because of the modified router, the module's controllers are accessible like a directory structure. Controllers may be loaded from the application/controllers sub-directories or the module/controllers sub-directories:

/module/hello -> /module/controllers/hello.php (index method)
/module/hello -> /module/controllers/hello/hello.php (index method)
/module/hello -> /module/controllers/hello/(default_controller).php (hello method)
/module/hello -> /module/controllers/module.php (hello method)
/module/hello -> /module/controllers/(default_controller).php (hello method)

If the requested module contains a routes.php config file it will automatically be added to the main routes.

Hierarchical controllers

To load hierarchical controllers you use the $this->load->controller() method. This method works similar to loading views with CodeIgniter, this method accepts the following parameters:

  • URI: a URI string pointing to the requested controller (and method). This function uses the same locating technique as explained above.
  • Parameters: an array containing the arguments for the requested method.
  • Return : a boolean indicating whether the output should be returned or show on the screen (default).

For example, this is our main controller where we pass the request to a sub-controller in the same module:

class Specials extends CI_Controller {
    
    public function index() {
        $this->load->controller('blogs/random', array('specials'));
    }
    
}

And the sub-controller contains the real method:

class Blogs extends CI_Controller {

    public function random($type) {
        ...
    }
    
}

codeigniter-hmvc-modules's People

Contributors

avenirer avatar harpanet avatar jenssegers avatar mrhyde avatar patroklo avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

codeigniter-hmvc-modules's Issues

Just Quick Question

If it is not in active development could I get my developer to look after it. And add a couple of things.

Tried finding your email did not want to make issue on here.

hi jenssegers

I use this codeigniter your extension
Found the session of flashdata not work
code:
$result=$this->data_mdl->edit_data($data,$edit_id);
$msg = $result > 0?'is ok!':'error';
$notify = $result > 0?'success':'error';
// $this->nsession->set_flashdata($notify, $msg);
// $this->nsession->keep_flashdata($notify);
$this->session->set_flashdata($notify, $msg);
$this->session->keep_flashdata($notify);
var_dump($this->session->all_userdata());

result:
array(7) {
["session_id"]=>
string(32) "774a709dff1073976144c8976902f1be"
["ip_address"]=>
string(9) "127.0.0.1"
["user_agent"]=>
string(72) "Mozilla/5.0 (Windows NT 6.1; WOW64; rv:27.0) Gecko/20100101 Firefox/27.0"
["last_activity"]=>
int(1393646597)
["user_data"]=>
string(0) ""
["flash:old:success"]=>
bool(false)
["flash:new:success"]=>
bool(false)
}

flash:old:success return FALSE
how to modify code?

Add Third Controller Level In Router

Hi I would like to know if it is possible to add in the router and loader a third level for controllers. Because at the moment can only get one level.

Example:
Can on get controller like below.
folder/controllers.php

Would like to be able to get
folder/folder/controller.php

And also can I change names of the $module and location on my one.
I am making template system that if open source on git hub my new version coming soon.

Error when loading controller using load->controller()

Greetings there and thanks for this great adding.
As the title says, when trying to load controller from another, I get the following error:
Unable to locate the specified class: Core.php
For the record, all other modules work just fine, locations are well set and all.
Thanks in advance and keep up the good work.

Does not work with ? in url

I would like to be able to use ? in url after the module name in url

http://localhost/project-hmvc-1/admin/?&route=common/dashboard

But will not allow the ? to work. It loads the home page and not the dashboard page

And if I remove the ? it works fine. I have tried it in routes.php and same issue just not allowing the ? after the module name

module name is admin.

Any ideas on how to make ? work I have tried to enable query strings to true but no luck. And I do not want to use codeigniter's enable_query_string.

Routes are not loading

Hi,
I have tried to print all routes in codeigniter but it is not loading from module directory. Please help!

Session library not found

Hi, when I try a blank installation of CI 3.1.7 and wanna use the session library I get:
Unable to locate the specified class: Session.php

I used the autoloader of CI:
$autoload['libraries'] = array('database', 'session');
Even if I use the drivers it seems not to work.

So far I have no idea why this isn't working.

hmvc modules form_validation session

Codeigniter version: 2.2.0
HMVC:codeigniter-hmvc-modules
The problem:
the form_validation set_value() function is not work!
My code :

load->controller('header',NULL,FALSE);?>
Name input type="text" name="name" value=""
Phone input type="text" name="phone" value=""
load->controller('footer',NULL,FALSE);?> The name and phone are Required If I just fill in the name wrong before can not obtain the value config/autoload.php $autoload['libraries'] = array('database','session','form_validation'); plese help me!

General Question

I can load your $this->load->controller in to the views I would like to know if it is possible to load it in as an array to another controller.

$data = array(
'header' => $this->load->controller('admin/common/header'),
'footer' => $this->load->controller('admin/common/footer')
);

$this->load->view('admin/template/common/login', $data);

Wont load view when load like this.

I just don't like how controllers are loaded on to views in hmvc they all should be loaded with in to make them dynamic.

I know you don't do work with codeigniter any more but would you know how I would get around this?

Relative path issue & fix

In core/Codeigniter.php lines 402, 408, there is a reference to this path:
APPPATH.'controllers/'.$RTR->directory.$class.'.php'
Normally, this works, but for users using custom loaders (like HVMC), it creates a problem on some machines.
It tries to access application/controllers/../modules/countries/controllers/Countries/index
Which on some machines translate correctly to application/modules/countries/controllers/Countries/index
And on some returns 404

A solution to that is using your own path resolver, something like:
Replace 402-408

function resolve_path($path) {
    $stack = [];
    foreach(explode('/', $path) as $segment)
        if($segment == "..")
            array_pop($stack);
        else
            $stack[] = $segment;
    return implode('/', $stack);
}
$controller_path = resolve_path(APPPATH.'controllers/'.$RTR->directory.$class.'.php');

if (empty($class) OR ! file_exists($controller_path))
{
    $e404 = TRUE;
}
else
{
    require_once($controller_path);

Turkish character issue

Hello,

I made all documents utf-8, its good at normal controller, But when i try to load module it shows turkish characters wrong.

Views not displaying

Views are not displaying except in the default controller

application/
modules/
welcome/
controllers/
welcome.php
views/
welcome_message.php
admin/
controllers/
admin.php
views/
admin_message.php

call $this->load->view('admin_message') from the admin controller will not display from the views folder inside admin only from the main application/views folder

call $this->load->view('welcome_message') from the welcome controller will display view that is in the welcome module views folder

Would be great to call partials from the module's own view folder

After install

Hey there @jenssegers,
Wanted to tell you about something that is occurring after installing. I have to create another 2 files in root/application/core called "MY_Model.php" and "MY_Controller.php". This is usual?

Thanks <3

HMVC How to support multiple folders?

like this

modules (folder)
test(folder)
--controller-a
--controller-b
--controller-c
frontend (folder)
-some module name (folder)
-- controller-1
-- controller-2
-- controller-3
back-end (folder)
-some module name (folder)
-- controller-1
-- controller-2
-- controller-3

Support for CI 3.X

Hello Team,

HMVC Modules made our coding life much organized and simple. We wanted to upgrade CI from 2.x to 3.x So, expecting support of HMVC Modules for CI 3.x versions as well.

module database

i can't override database configuration in application/config/database.php using database module in modules/module/config/database.php

CI_Session inconsistency

Hello,
first : nice job, these classes were very helpful ๐Ÿ‘
I had a problem with the session class due to its re-instanciation.
Let's say I have a parent controller calling several children controllers (ctrl_a & ctrl_b).
if you load the session library in ctrl_a and set data in it :
$this->session->set_userdata('arbitrary_key_a','arbitrary_value_a');
then in the ctrl_b you load session library and once more add data :
$this->session->set_userdata('arbitrary_key_b','arbitrary_value_b');
Only the datas saved in ctrl_b will persist because the session library is not a singleton !
So I had to override the CI_Session class, modify the $userdata property and set it to static and force sess_read to merge the cookie's data to the static $userdata array.

Sub Directory File For HMVC

Hi Matthew here. I have found some code that I have been putting together. To make your HMVC Codeigniter be able to have sub directories but have a couple of bugs in it.

If you have got time could you look at it?

If you could fix it the you can add it to your hmvc. Its only couple minor fixes I think. It would be a good edition to it if can make it work.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.