Welcome Guest, Not a member yet? Register   Sign In
Modular Extensions - Version 4.3

[eluser]wiredesignz[/eluser]
Using $CI means the model is loaded into the controller, not the module system.

[eluser]codex[/eluser]
[quote author="wiredesignz" date="1209630504"]Using $CI means the model is loaded into the controller, not the module system.[/quote]

- read the user guide -> did that
- read the wiki -> did that
- search the forum ->did that

What is the proper way to load a model into a module library system then? Wouldn't this work?:

Code:
$this->model =& modules::load('user_model');

Throws an error:

Code:
A PHP Error was encountered

Severity: Notice

Message: Only variable references should be returned by reference

Filename: helpers/modules_helper.php

Line Number: 43

[eluser]wiredesignz[/eluser]
To be honest I think your application is becoming too complicated if you need to load a module model into a module library, there must be some more simple way to get the same result.

But for the record you can try this, but I'm not sure if it still works in the current version.

You can specify which type of class the module loader should look for:
Code:
$this->model =& modules::load('user_model', 'models/');
If this errors then add the path also: 'admin/user_model'

[eluser]codex[/eluser]
[quote author="wiredesignz" date="1209659282"]To be honest I think your application is becoming too complicated if you need to load a module model into a module library, there must be some more simple way to get the same result.[/quote]

Well, let me explain what I'm trying to do, maybe I am making it way too complicated indeed.

This particular module called 'users' handles all userstuff: signup, login, activate account, forgot password etc etc. To avoid the controller getting to bloated I moved most related functions to the 'Userlib'-library, which also does db inserts, updates etc. DB calls I have put into a usermodel which I then need to load into the userlib in order to use it.

I guess to circumvent these problems I *could* put all functions into the module controller.

EDIT: $this->model =& modules::load('user_model', 'public/users/models/'); seems to be working!

[eluser]wiredesignz[/eluser]
Cool, glad it worked. Wink

Another little feature you may or may not know is that modules can have seperate method files. So the module can be extended without clutter.

Create a methods directory under the module name:
Code:
module_name/
    - config/
    - controllers/
        - users.php
    - helpers/
    - language/
    - libraries/

    - methods/
        - signup.php
        - forgot_pwd.php
        - register.php

    - models/
    - plugins
    - views/

methods can be loaded in the module: $this->load->method('some_method'); and used like libraries or they can be called as part of the module, they will be loaded and run automatically.

modules::run('module', $data, 'some_method')

Methods need to extend YOUR module controller class but they dont need to have a constructor and they have access to everything the module does.

I hope this is some help Tongue

[eluser]arume[/eluser]
I don't know if I am doing something wrong, but this
Code:
$this->parser->parse('module_name', $data);
inside a module do an error:
Code:
Unable to locate the requested file: /views/module_name.php

This is OK if I write:
Code:
$this->load->view('module_name', $data);

Is something wrong in my code?
If not, can I use the CI parser with HMVC inside modules?

[eluser]wiredesignz[/eluser]
Use this parser helper.
Code:
<?php if (!defined('BASEPATH')) exit('No direct script access allowed');
/**
* Parser Static Helper Class
*
* Adapted from CodeIgniter Parser Class
* @copyright    Copyright (c) 2006, EllisLab, Inc.
* @license    http://ellislab.com/codeigniter/user-guide/license.html
*
* Allows the use of functions in parsed templates ie: {@site_url("home"/{page})}
*
* Place this file in application/helpers as parser_helper.php
* and load as needed.
*
* Usage:
* $parsed_view = parser::parse($view, $data);
*
* Version 0.3 (c) Wiredesignz 2008-02-28
**/
class Parser
{
    function parse($view, $data = array())
    {
        if ($template = $this->load->view($view, $data, TRUE))
        {            
            $template = parser::_parse($template, $data);
                        
            while(preg_match('/\{\@(\w*)\("(.*)"\)\}/siU', $template, $match))
            {
                $template = str_replace($match[0], $match[1]($match[2]), $template);
            }
                        
            return $template;
        }
    }
    
    function _parse($template, $data)
    {
        foreach ($data as $key => $val)
        {
            if (is_array($val))
            {                
                $template = parser::_parse_pair($key, $val, $template);        
            }
            else
            {
                $template = parser::_parse_single($key, (string)$val, $template);
            }
        }
        return $template;
    }
    
    function _parse_single($key, $val, $string)
    {
        return str_replace('{'.$key.'}', $val, $string);
    }

    function _parse_pair($variable, $data, $string)
    {    
        if (FALSE === ($match = parser::_match_pair($string, $variable)))
        {
            return $string;
        }

        $str = '';
        foreach ($data as $row)
        {
            $temp = $match['1'];
            foreach ($row as $key => $val)
            {
                if ( ! is_array($val))
                {
                    $temp = parser::_parse_single($key, $val, $temp);
                }
                else
                {
                    $temp = parser::_parse_pair($key, $val, $temp);
                }
            }
            
            $str .= $temp;
        }
        
        return str_replace($match['0'], $str, $string);
    }

    function _match_pair($string, $variable)
    {
        if (!preg_match('/{'.$variable.'}(.*){\/'.$variable.'}/s', $string, $match))
        {
            return FALSE;
        }
        
        return $match;
    }
}

[eluser]arume[/eluser]
Thank you. I learnt a lesson with the use of $this in a static method. By the way, you have an error. It must be:
Code:
if (!preg_match('/{'.$variable.'}(.*){\/'.$variable.'}/s', $string, $match))

I have reached another solution (not so elegant as yours) in the meantime:

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

class MY_Parser extends CI_Parser
{
    function parse($template, $data, $return = FALSE, $object = NULL)
    {
        if ($object === NULL)
        {
            $object =& get_instance();
        }

        $template = $object->load->view($template, $data, TRUE);

        if ($template == '')
        {
            return FALSE;
        }

        foreach ($data as $key => $val)
        {
            if (is_array($val))
            {
                $template = $this->_parse_pair($key, $val, $template);
            }
            else
            {
                $template = $this->_parse_single($key, (string)$val, $template);
            }
        }

        if ($return == FALSE)
        {
            $object->output->final_output = $template;
        }

        return $template;
    }
}
And in the module:
Code:
return $this->parser->parse('view', $data, false, $this);

Maybe it can be useful to somebody.

[eluser]wiredesignz[/eluser]
Corrected thanks.

[eluser]wiredesignz[/eluser]
Version 4.0.29 is available on the wiki

Corrected a bug in modules::load to return a valid reference.




Theme © iAndrew 2016 - Forum software by © MyBB