开发者

How do I load my models inside a codeigniter hook file

开发者 https://www.devze.com 2023-01-09 06:05 出处:网络
Here\'s my issue. I am building an application that requires filters. I have gotten the filter system working and I can even pinpoint the actual method i want to access @ the moment. The issue is that

Here's my issue. I am building an application that requires filters. I have gotten the filter system working and I can even pinpoint the actual method i want to access @ the moment. The issue is that I cannot access the CI core from the filter file. I have searched across the web and while I have found some suggested solutions like "Dipping into CI", they are not recommended because they can cause unstable PHP applications.

A sample filter in my application is as written below

class Trust_filter extends Filter {
  function before() {
    $this -> ci = &get_instance();
    if ($this -> ci -> auth -> is_user_active()) {
      $this -> ci -> load -> model("trust_model", "trustmanager");
      if ($this -> trustmanager -> verify()) {
        echo "##090##";
      }
    }
  }
}

The above code doesn't work because the CI开发者_运维技巧 object is a non-object. I can't get an instance and I have spent a whole night on this fruitlessly. I am using CodeIgniter 1.7.1 .Any help will be appreciated.

Edit: I have found an answer to the question. I extended the filters system to work after the constructor has been created to ensure I would have access to the CI superobject. My code works perfectly too. thanks all but I am still interested in your solutions.


$ci=&get_instance();

$ci->load->model("auth");

$ci->auth->login();

This type you can get or access model in hooks files.


There's a lot of weirdness in codeigniter in terms of when things are loaded and created. In terms of hooks often you will find libraries/helpers/config/models are not loaded until post controller construction it can be a pain in a lot of cases (like this one).

Try calling this hook as post controller construction. At that point you will definitely be able to get access to the ci instance but I'm not sure about access to models.

Also note that models should only really be loaded/used from inside controllers (one reason why they might not work in precontroller hooks). Codeigniter and a lot of programmers get around this by creating model like behavior within libraries (the session library for example).

0

精彩评论

暂无评论...
验证码 换一张
取 消