PostMans
PostMans

Reputation: 338

CodeIgniter - HMVC, Helpers and Undefined property

I use WireDesignz HMVC and have a KERNEL_Controller (which is actually the MY_Controller with another prefix) using CodeIgniter 3.0.6

class KERNEL_Controller extends MX_Controller {
    public $myvar;

    public function __construct() {
            parent::__construct();

            //Load core Model
            $this->load->model('Core_model');

            $this->myvar = 'test';
    }
} 

Now I have an Helper which does the following

function Editor($id) {
    $ci =& get_instance();

    echo $ci->myvar;
} 

When I call the Editor in a view, I get

Severity: Notice
Message: Undefined property: CI::$myvar
Filename: helpers/editor_helper.php

Same when calling a method in the Editor Helper

In the KERNEL_Controller I have

public function DoTest() {
    echo '1';
} 

And in the Editor_helper

function Editor($id) {
    $ci =& get_instance();    
    echo $ci->DoTest();
} 

I get, when calling the Editor in a view

Type: Error
Message: Call to undefined method CI::DoTest()

But when I call to a Model in the Editor Helper

function Editor($id) {
    $ci =& get_instance();

    $result_content = $ci->Core_model->GetLanguages(1);
    print_r($result_content);
} 

I do get (in this case) an array with languages back.

Upvotes: 0

Views: 1484

Answers (2)

santutu
santutu

Reputation: 129

Oh yes!! I'm the man, I fixed it! Thought about maybe creating my "own" get_instance() system, here's what I did:

class MY_Controller extends MX_Controller
{
    public static $instance;
    function __construct()
    {
        self::$instance || self::$instance =& $this;
        ...
    }
}

Then in the library, or in the helper or wherever funky place you need to use it:

$CI =& MY_Controller::$instance;

NOTE that if you autoload a library, MY_Controller::$instance won't work if it's in the library's __construct(), as MY_Controller is not defined yet

https://stackoverflow.com/questions/16433210

Upvotes: 1

ksimka
ksimka

Reputation: 1455

According to https://bitbucket.org/wiredesignz/codeigniter-modular-extensions-hmvc you have to use something like

<?php
/** module and controller names are different, you must include the method name also, including 'index' **/
modules::run('module/controller/method', $params, $...);

/** module and controller names are the same but the method is not 'index' **/
modules::run('module/method', $params, $...);

/** module and controller names are the same and the method is 'index' **/
modules::run('module', $params, $...);

/** Parameters are optional, You may pass any number of parameters. **/

I don't know how your module is called, but you can try

modules::run('module/KERNEL/DoTest');

in order to invoke method DoTest of KERNEL controller.

Upvotes: 0

Related Questions