• 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
hooks and controllers

#1
[eluser]yusufdestina[/eluser]
I'm using Codeigniter 1.7 and HMVC (the latest release for php5).
Before I used modular extension for php4 and had no problems with it.

After upgrading my site breaks.

hook config
Code:
$hook['post_controller_constructor'][] = array('class'    => 'language_hook',
                                             'function' => 'setLanguage',
                                             'filename' => 'language_hook.php',
                                             'filepath' => 'hooks');
hook file
Code:
class language_hook{
    var $CI;
    var $sitelang = array();
    var $validLanguages = array(    array('code' => 'nl_BE', 'url' => 'nl', 'human' => 'nederlands'),
                                    array('code' => 'fr_FR', 'url' => 'fr', 'human' => 'frans'),
                                    array('code' => 'de_DE', 'url' => 'de', 'human' => 'duits'));        
    function __construct()
    {
        log_message('debug', 'Msg: Hook initialized');
    }
    
    public function setLanguage()
    {
        log_message('debug', 'Msg: Hook setLanguage');
        $this->CI =& get_instance();
        $lang = $this->CI->uri->segment(1);
        $lang = strtolower($lang);
        foreach($this->validLanguages as $langItem)
        {
            if(in_array($lang, $langItem))
            {
                log_message('debug', 'Msg: Hook in array');
                $this->CI->sitelang['code'] = $langItem['code'];
                $this->CI->sitelang['url'] = $langItem['url'];
                $this->CI->sitelang['human'] = $langItem['human'];
            }
        }
    }
}

Now in a method of my controller I can use
Code:
class Home extends Controller {

        function Home()
        {
            parent::Controller();
            $this->load->model('pages_model', 'page');
            $data['pageTitle'] = "dummy";    
            $this->load->vars($data);
        }
        function index()
        {
            $this->lang->load('site', $this->sitelang['code']);
            ...
        }
}

Problem is that "$this->sitelang" does not exist anymore after upgrading to modular extension for php5

I get this error:
Code:
DEBUG - 2008-11-19 15:45:28 --> Config Class Initialized
DEBUG - 2008-11-19 15:45:28 --> Hooks Class Initialized
DEBUG - 2008-11-19 15:45:28 --> URI Class Initialized
DEBUG - 2008-11-19 15:45:28 --> Router Class Initialized
DEBUG - 2008-11-19 15:45:28 --> Output Class Initialized
DEBUG - 2008-11-19 15:45:28 --> Input Class Initialized
DEBUG - 2008-11-19 15:45:28 --> XSS Filtering completed
DEBUG - 2008-11-19 15:45:28 --> Global POST and COOKIE data sanitized
DEBUG - 2008-11-19 15:45:28 --> Language Class Initialized
DEBUG - 2008-11-19 15:45:28 --> Language file loaded: language/fr_FR/frans_lang.php
DEBUG - 2008-11-19 15:45:28 --> Loader Class Initialized
DEBUG - 2008-11-19 15:45:28 --> Home Controller Initialized
DEBUG - 2008-11-19 15:45:28 --> Config file loaded: config/theme.php
DEBUG - 2008-11-19 15:45:28 --> Helper loaded: url_helper
DEBUG - 2008-11-19 15:45:28 --> Helper loaded: text_helper
DEBUG - 2008-11-19 15:45:28 --> Helper loaded: theme_helper
DEBUG - 2008-11-19 15:45:28 --> Database Driver Class Initialized
DEBUG - 2008-11-19 15:45:28 --> Session Class Initialized
DEBUG - 2008-11-19 15:45:28 --> Helper loaded: string_helper
DEBUG - 2008-11-19 15:45:28 --> Session routines successfully run
DEBUG - 2008-11-19 15:45:28 --> File loaded: C:\wamp\www\repo/system/application/models/pages_model.php
DEBUG - 2008-11-19 15:45:28 --> Model Class Initialized
ERROR - 2008-11-19 15:45:28 --> Severity: Notice  --> Undefined property: Home::$sitelang C:\wamp\www\repo\system\application\controllers\home.php 43
The hook isn't executed anymore.
I've tried with pre_controller, post_controller,... no success
I hope someone can help me out Smile tnx for your time and help!

#2
[eluser]xwero[/eluser]
Wouldn't it be better if you add it to the config instead of adding the sitelang variable to the super object?
Code:
if(in_array($lang, $langItem))
{
    log_message('debug', 'Msg: Hook in array');
    $this->CI->config->set_item('lang_code', $langItem['code']);
    $this->CI->config->set_item('lang_url'], $langItem['url']);
    $this->CI->config->set_item(['lang_human'], $langItem['human']);
}
And you should add the default language too to make sure the config items exist.

#3
[eluser]Sander Versluys[/eluser]
I have the exact same problem. And when searching on the forum i came across this post and stumbled upon on very similar code. Funny... Yes a language hook, and indeed also for belgium!

For me, the get_instance() function returns NULL.

Simplified code:

Class:
Code:
class Language_hook {

    var $CI;

    function set_language() {

        $this->CI =& get_instance();
        
        var_dump($this->CI);

    }
}

Config:
Code:
$hook['pre_controller'] = array(
                                'class'    => 'Language_hook',
                                'function' => 'set_language',
                                'filename' => 'Language_hook.php',
                                'filepath' => 'hooks'
                                );


Owkay this throws a 'headers modified' error, but should dump the var as usual.

Any tips are greatly appreciated! :roll:

#4
[eluser]xwero[/eluser]
The CI super object isn't defined before the pre_controller hook so calling the object is useless. When the post_controller_constructor hook is called the CI object is defined and classes are already added.

#5
[eluser]Sander Versluys[/eluser]
Owkay, fixed it myself:

When in the pre_controller fase, the get_instance() function returns null. If I hook onto 'post_controller_constructor' fase, it works...


UPDATE: Did not refresh my browser, so I didn't see your answer. I indeed discovered thet myself... Thanks anyway! ;-)


Digg   Delicious   Reddit   Facebook   Twitter   StumbleUpon  


  Theme © 2014 iAndrew  
Powered By MyBB, © 2002-2020 MyBB Group.