Welcome Guest, Not a member yet? Register   Sign In
Modular HMVC - Libraries on Steroids [Updated x3]
#11

[eluser]wiredesignz[/eluser]
This is an example of a base controller using the _remap() method to load modules HMVC style.

Code:
<?php if (!defined('BASEPATH')) exit('No direct script access allowed');

class Base extends Controller
{
    function Base()
    {
        parent::Controller();
        $this->load->library('modules');
    }
    
    function _remap()
    {
        $path   = $this->uri->segment(2).'/';
        $module = $path.$this->uri->segment(3);
        $action = $this->uri->segment(4);

        $this->modules->load($module, $action);
    }
}

Code:
http://127.0.0.1/base/admin/products/edit

The admin/products module:
Code:
<?php if (!defined('BASEPATH')) exit('No direct script access allowed');

class Products extends Modules
{
    function Products($action = NULL)
    {
        parent::Modules();
        $this->load->model('products_model', 'products');
        
        if ($action) $this->$action();
    }
    
    function add()
    {
        echo 'admin/products/add';
    }
    
    function edit()
    {
        echo 'admin/products/edit';
    }
}
#12

[eluser]Edemilson Lima[/eluser]
I find your implementation to modules very elegant and follow the CI standard better than any other I have found. But I would like to know what do you have against extending the core. Using $this->modules->load() is in some way better than $this->load->module()? Personally I don't like changes to the core, but I don't see anything bad with extending it.

How do you call the functions setup(), add() and edit() in your examples above? Also, there is always necessary to have a contructor or the index() function could be called by default?

Code:
class Modules
{    
    function Modules()
    {
I noticed that "Controller" and "Model" are in singular. To keep the same standard, wouldn't be better if was "Module" instead of "Modules"?
Code:
class Product extends Controller {
class Products_model extends Model {
class Product_list extends Module {
Well, it is just a suggestion. Smile

Code:
class _login extends Modules //as a psuedo-controller
Just to satisfy my curiosity... Do you mean the urban word psuedo or the english/latin word pseudo?
#13

[eluser]Sam Dark[/eluser]
pseudo is pseudo == almost
#14

[eluser]Edemilson Lima[/eluser]
Pseudo means false. But psuedo have another meaning, I think.
#15

[eluser]zdknudsen[/eluser]
A little competition, eh? Great!

If I understand your library right, I will try to explain how I believe our solutions differ.

The philosophy behind Matchbox, was to give developers additonal possibilities in means of organization, whithout having to change which methods to call in order to load resources, etc. Effectively, what this means is that developers are able to take any standard CI application they've made, split it into smaller components and place said components in module folders and still have them work (e.g. no need to go back into the files and change the resource-loading methods).

The main thoughts behind "Modular Separation - Nested MVC" are to first of all not alter the core (which Matchbox does in a high degree) and enable one to use HMVC. Both of which probably wont ever make it into Matchbox, which is why this is great for the end-user who can choose whatever they see fit Smile

(By the way, would you consider renaming your library to something else than Modular Separation? That is what my previous version of Matchbox was called, just to avoid any confusion. Or perhaps it doesn't matter. Just think about it Smile )
#16

[eluser]Edemilson Lima[/eluser]
It could be called "Pseudo-HMVC". :-)

I don't care about the library name, but I do prefer to use:
Code:
class Products extends Module {
Than:
Code:
class Products extends SomeFussName {
"Module" or "Partial" are the most intuitive names, I think.
#17

[eluser]wiredesignz[/eluser]
Quote: But I would like to know what do you have against extending the core.

@Edemilison: If I want to use Matchbox also, then other core extensions would cause lots of trouble to make things work together.

I prefer my Modules Library, it is very versatile, and I can use Matchbox also.

You can rename the Modules library to anything you like, I like Modules because I load many modules at once, and a Module is the loaded object, not the library itself.

Yes you must always have a constructor, so your Module gets extended properly, you do not need an index() method, if you want a default function to run after loading then call it from your constructor.

Calling functions is the same format as you would call a Model or Library, I prefer to use render() when I want the Module to output its view, like so:
Code:
<?php echo $this->search->render(); ?>

// in a controller you would use:

$this->search->update();  //calls the search module to grab post data & update

@Zach: I was being cheeky with that name Tongue, Modular HMVC is more appropriate.

I hope you all try it out, I have been experimenting with it all day and have found many uses. It's just up to your imagination.

Maybe it should be called Libraries on Steroids Tongue
#18

[eluser]zdknudsen[/eluser]
Ooh, I didn't realize our libraries could work together Smile Awesomenes.

Namewise you could also go with "Modular and Nested HMVC".
#19

[eluser]wiredesignz[/eluser]
Yes Zach, your Modular MVC apps can then contain my Modules also. Smile
#20

[eluser]wiredesignz[/eluser]
Quote:How do you call the functions setup(), add() and edit() in your examples above?

@Edmilison: The controller uses the _remap() method as detailed in the CI user guide. It grabs the first two URI segments to determine what module to load and what function to call.

The module then grabs any data from the remaining URI segemnts and does its thing. thats HMVC Smile




Theme © iAndrew 2016 - Forum software by © MyBB