Part of the EllisLab Network
This thread is a discussion for the wiki article: Modular Extensions - HMVC
   
6 of 36
6
Modular Extensions - HMVC
Posted: 07 April 2009 02:07 PM   [ Ignore ]   [ # 76 ]  
Grad Student
Avatar
Rank
Total Posts:  54
Joined  03-19-2008

Wish there were more inline documentation to ME Classes & Methods. Now digging into your code i wonder why you treat $module as an array in Modules->load function?

Is this done to enable autoload() functionality (load an array of modules)?

UPDATE:

I would like to sudgest a small unobtrusive addition to Modules and Controller Class (or discuss some other ways to implement similar functionality).

So let me explain: It is very convenient that besides running modules with modules::run() you can actually load a module or a module sub-controller as library to access it’s functionality from other modules and controllers.

But there’s an inconvenience -  you can not pass any additional data along when loading modules as library.
This functionality is very much needed & missing. Sometimes you just need to pass additional data to __construct() to instantiate a library instance.

So this is just a quick fix after some digging your code. This does not change functionality of modules loading as well as loading arrays of modules.

What do you think?

Controller.php Libarry

/** Load a module controller **/
    /* Added $module_vars to get an array of parameters for module constructor*/

    
public function module($module$module_vars=array())
    
{
        
if (is_array($module)) 
            return 
self::modules($module);
        
        
$ci modules::$registry[$this->_class];

        
/* get the controller name */
        
$controller strtolower(end(explode('/'$module)));

        
/* pass $module_vars to module loader */
        
(isset($ci->$controller)) OR $ci->$controller modules::load($module$module_vars);
            
        return 
$ci->$controller;
    

 

Modules.php Libarry

/** Load a module controller **/
    
public static function load($module$module_vars=array())
    
{    
        
(is_array($module)) AND list($module$params) = each($module) OR $params NULL;
        
        
/* get the controller class name */
        
$class strtolower(end(explode('/'$module)));

        
/* is controller in registry? */
        /* if $module_vars are passed - we need a separate controller instance - don't look up in module regestry*/
        
if (isset(self::$registry[$class]) && empty($module_vars)) 
            return 
self::$registry[$class];
            
        
/* get the module name */
        
list($module) = explode('/'$module);

        
/* find the module controller */
        
list($module$controller) = Router::locate(array($module$class));

        if (
$module === FALSE) return;

        
/* set the module directory */
        
self::$home $module;
        
        
$path = ($module) ? MODOFFSET.$module.'/controllers/' NULL;

        
/* load the module controller class */
        
self::load_file($controllerAPPPATH.'controllers/'.$path);
        
        
$class ucfirst($controller);

        
/* create the new controller */

        /* added some logic to avoid $params/$module_vars collision */
        
if(empty($module_vars)) $controller = new $class($params);
        else 
$controller = new $class($module_vars);
        
        return 
$controller;
    


Looking forward to hear any thoughts & ideas!  grin

Cheers! 11

Profile
 
 
Posted: 13 April 2009 02:36 PM   [ Ignore ]   [ # 77 ]  
Summer Student
Avatar
Total Posts:  25
Joined  06-26-2008

Hi guys, no matter what I do I can never seem to get this to work tried both versions without any luck so far each time getting a multitude of errors from a clean install of CI, could someone post a quick how-to install guide maybe as it seems obvious it must be ME doing something wrong (thats me as in I, and not ME as in Modular Extensions lol!)

PS. Yeah that was quite a lame joke smile

Thanks CI rocks!

Profile
 
 
Posted: 13 April 2009 04:43 PM   [ Ignore ]   [ # 78 ]  
Grad Student
Avatar
Rank
Total Posts:  54
Joined  03-19-2008
playaz - 13 April 2009 06:36 PM

Hi guys, no matter what ...

Hi,

There’s a nice step-by-step guide to installing ME in the Wiki.
Have you seen it?

Profile
 
 
Posted: 23 April 2009 04:00 PM   [ Ignore ]   [ # 79 ]  
Summer Student
Avatar
Total Posts:  11
Joined  03-04-2009

Hi!
I have a question and would be grateful if someone answer.
With ME 5.2.01 I could call common view from module and now, when use ME 5.2.07, it doesn’t work for me.
Where am I mistaken??? Or how to create the same functionality? - (module is called as part of the common template, uri is www.domain_name/module_name).

file: application/modules/module_name/controller/module_name.php

class Module_name extendes Controller{
function index(){
  $data[‘main’] = ‘module_name_v’;
  $this->load->view(‘tpl/template_v’, $data);
}
template is situated in application/views/tpl/template_v.php
and there’s I have <?php $this->load->view($main); ?>

Thanks!

Profile
 
 
Posted: 23 April 2009 10:54 PM   [ Ignore ]   [ # 80 ]  
Sr. Research Associate
Avatar
RankRankRankRankRank
Total Posts:  3216
Joined  06-10-2007

Modular extensions 5.2.08 is available on the wiki.

Corrected the problem loading a view in view described above. Thanks yuga wink

 Signature 

URI Language Identifier | Modular Extensions - HMVC | View Object | Widget plugin | Access Control library

Profile
 
 
Posted: 24 April 2009 02:59 AM   [ Ignore ]   [ # 81 ]  
Summer Student
Avatar
Total Posts:  11
Joined  03-04-2009

That’s great!!
I’m happy )))
Thanks!

Profile
 
 
Posted: 24 April 2009 03:07 AM   [ Ignore ]   [ # 82 ]  
Grad Student
Avatar
Rank
Total Posts:  54
Joined  03-19-2008
wiredesignz - 24 April 2009 02:54 AM

Modular extensions 5.2.08 is available on the wiki.

Corrected the problem loading a view in view described above. Thanks yuga wink


Hi!

Would you also be so king to say few words about my suggestion above? Also someone here on the forum posted that you stop supporting ME because of moving to another framework. Is that true?

Profile
 
 
Posted: 24 April 2009 04:39 AM   [ Ignore ]   [ # 83 ]  
Sr. Research Associate
Avatar
RankRankRankRankRank
Total Posts:  3216
Joined  06-10-2007

@dmitrybelyakov

Modules have always been able to accept parameters.

$this->load->module('module_name'$params);

or 

Modules::load(array('module_name' => $params)); 

I still support Modular Extensions as you can see.

 Signature 

URI Language Identifier | Modular Extensions - HMVC | View Object | Widget plugin | Access Control library

Profile
 
 
Posted: 24 April 2009 04:46 AM   [ Ignore ]   [ # 84 ]  
Grad Student
Avatar
Rank
Total Posts:  54
Joined  03-19-2008
wiredesignz - 24 April 2009 08:39 AM

I still support Modular Extensions as you can see.

Cool! Glad to see you back here. I was kinda disappointed because i did a lot of work in ME. How was Yii compared to CI by the way? smile

Profile
 
 
Posted: 24 April 2009 04:57 AM   [ Ignore ]   [ # 85 ]  
Sr. Research Associate
Avatar
RankRankRankRankRank
Total Posts:  3216
Joined  06-10-2007

Yii is far more advanced than CodeIgniter in terms of inbuilt functionality and once you learn it’s fundamentals, is also very easy to use.

However, I still enjoy working with CI and this community.

 Signature 

URI Language Identifier | Modular Extensions - HMVC | View Object | Widget plugin | Access Control library

Profile
 
 
Posted: 24 April 2009 05:42 AM   [ Ignore ]   [ # 86 ]  
Grad Student
Avatar
Rank
Total Posts:  54
Joined  03-19-2008
wiredesignz - 24 April 2009 08:39 AM

Modules have always been able to accept parameters.


Still this doesn’t work out. First i thought it was my old 5.1.40 version but with the new 5.2.08 this does not work still.

I tried both your examples but still can’t access $params inside a Module controller.
My configuration routes everything to the default module, were in __construct() i call for Module:

$params=array();
$params['tree_id']="some parameter to pass";

//Tried both this variants.
$this->_path=$this->load->module('test_module'$params);
Modules::load(array('test_module' => $params)); 

And the Test_Module controller looks like this:

<?php
class Test_Module extends Controller
{
    
function __construct()
    
{      
        parent
::Controller();
        echo 
"<h2>Running Test Module</h2>";
        echo 
"<pre>";
        
print_r($this->params);
        
print_r($this->tree_id);
        
print_r($params);
        
print_r($tree_id);        
        die(
'RUNNING MODULE WITH PARAMS');
    
}

?> 
Profile
 
 
Posted: 24 April 2009 06:37 AM   [ Ignore ]   [ # 87 ]  
Sr. Research Associate
Avatar
RankRankRankRankRank
Total Posts:  3216
Joined  06-10-2007

Parameters are passed to the constructor of the class.

<?php
class Test_Module extends Controller
{
    
function __construct($params)
    
 Signature 

URI Language Identifier | Modular Extensions - HMVC | View Object | Widget plugin | Access Control library

Profile
 
 
Posted: 24 April 2009 07:15 AM   [ Ignore ]   [ # 88 ]  
Grad Student
Avatar
Rank
Total Posts:  54
Joined  03-19-2008

Yup, tried that in the first place. Now i got it working, but can’t figure out were i went wrong before smile Anyway thanks a lot for your help!

By the way, there’s one more thing: 

I was using a top-level module that runs all modules on demand (like default controller), in this module i created a _CI variable and assign it by reference.

Then i did some processing and created some ‘environment’ parameters that were set in default module and used application-wide by other modules like calling $this->_CI->request(‘current_language’);

Since last changes it’s impossible set this _CI variable any more because of __get()  (returns ‘Cannot assign by reference to overloaded object’).

The new Module Object printout looks pretty cool without all the CodeIgniter in it, but still how can I access the methods of default module.

Maybe you can suggest how to do it ‘properly’ with the new 5.2.08 or how it was intended to be done?

smile

Profile
 
 
Posted: 24 April 2009 07:35 AM   [ Ignore ]   [ # 89 ]  
Sr. Research Associate
Avatar
RankRankRankRankRank
Total Posts:  3216
Joined  06-10-2007

You may use CI::$APP instead of get_instance() in ME 5.2 but don’t assign objects by reference as PHP5 does this by default. You may assign variables by reference if needed.

 Signature 

URI Language Identifier | Modular Extensions - HMVC | View Object | Widget plugin | Access Control library

Profile
 
 
Posted: 24 April 2009 08:25 AM   [ Ignore ]   [ # 90 ]  
Grad Student
Avatar
Rank
Total Posts:  54
Joined  03-19-2008
wiredesignz - 24 April 2009 11:35 AM

You may use CI::$APP instead of get_instance() in ME 5.2 but don’t assign objects by reference as PHP5 does this by default. You may assign variables by reference if needed.

Yes that worked, now i can access the class variables that were set in the default module, but still no assess to default module method’s. Is there a way to access them from other modules?

Profile
 
 
   
6 of 36
6