Codeigniter 中出现错误

发布于 2024-11-19 11:32:06 字数 1365 浏览 0 评论 0原文

我正在我的 codeigniter 应用程序中自动加载一个名为“render”的库。 该库的完整代码是:

class Render extends CI_Controller {
    public function template($template, $view, $extra_css, $extra_js) {
        $data = array();
        if (isset($view)) {
            $data['view'] = $view;
        }
        if (isset($extra_css)) {
            $data['extra_css'] = $extra_css;
        }
        if (isset($extra_js)) {
            $data['extra_js'] = $extra_js;
        }
        $template = $this->load->view("templates/$template", $data, TRUE);
        echo $template;
    }

}

该库工作正常,但问题是每当我手动加载该库或通过编辑自动加载文件时,当我在任何控制器中加载任何模型时都会收到错误。

A PHP Error was encountered

Severity: Notice

Message: Undefined property: Home::$my_model_name

Filename: controllers/home.php

Line Number: 11

这里第 10 行和第 11 行是:

$this->load->model('my_model_name');
$this->my_model_name->my_model_method();

我也尝试使用:

$this->load->model('my_model_name', 'My_model');
$this->My_model->my_model_method();

我的控制器“home”代码是:

class Home extends CI_Controller {
    function __construct() {
        parent::__construct();
    }
    public function index()
    {
        $this->load->model('my_model_name');
        $this->my_model_name->index();
    }
}

我尝试向我的库添加 __construct() 方法,但仍然没有运气。

i'm autoloading a library named as 'render' in my codeigniter app.
the full code of the library is :

class Render extends CI_Controller {
    public function template($template, $view, $extra_css, $extra_js) {
        $data = array();
        if (isset($view)) {
            $data['view'] = $view;
        }
        if (isset($extra_css)) {
            $data['extra_css'] = $extra_css;
        }
        if (isset($extra_js)) {
            $data['extra_js'] = $extra_js;
        }
        $template = $this->load->view("templates/$template", $data, TRUE);
        echo $template;
    }

}

this library working fine but the problem is that whenever i load this library manually or by editing autoload file, i get an error when i load any model in any of my controller.

A PHP Error was encountered

Severity: Notice

Message: Undefined property: Home::$my_model_name

Filename: controllers/home.php

Line Number: 11

here line number 10 and 11 are :

$this->load->model('my_model_name');
$this->my_model_name->my_model_method();

and i also tried to use :

$this->load->model('my_model_name', 'My_model');
$this->My_model->my_model_method();

My controller "home" code is :

class Home extends CI_Controller {
    function __construct() {
        parent::__construct();
    }
    public function index()
    {
        $this->load->model('my_model_name');
        $this->my_model_name->index();
    }
}

i tried to add a __construct() method to my library but still no luck.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

好倦 2024-11-26 11:32:06

通过这样做,

class Render extends CI_Controller

您不是在创建一个库,而是一个控制器!为了创建一个库,只需创建该类并将其放入库文件夹中即可。

在你的库中,如果你想使用 CI 的加载器来加载模型,例如,你需要实例化主 CI 类。

$CI = & get_instance();

类似于 (file application/libraries/render.php) :

    class Render {

      var $CI;

      function __construct()
      {
        $this->CI = &get_instance();
      }

       public function template($template, $view, $extra_css, $extra_js) {
        $data = array();
        if (isset($view)) {
            $data['view'] = $view;
        }
        if (isset($extra_css)) {
            $data['extra_css'] = $extra_css;
        }
        if (isset($extra_js)) {
            $data['extra_js'] = $extra_js;
        }
        $template = $this->CI->load->view("templates/$template", $data, TRUE);
        return $template;
    }
}

然后您可以 $CI->load 您的库、模型、其他库等中想要的所有内容。
有关详细说明,请参阅利用库中的 CI 资源。然后,您可以按照通常的方式调用您的库,$this->load->library('render'),然后$this->render->whatever();

By doing

class Render extends CI_Controller

you're not creating a library, but a controller! In order to create a library, just create the class and put it into the libraries folder.

Inside your library, if you want to use CI's loader to load models, for ex., you need to instantiate the main CI class.

$CI = & get_instance();

Something like (file application/libraries/render.php) :

    class Render {

      var $CI;

      function __construct()
      {
        $this->CI = &get_instance();
      }

       public function template($template, $view, $extra_css, $extra_js) {
        $data = array();
        if (isset($view)) {
            $data['view'] = $view;
        }
        if (isset($extra_css)) {
            $data['extra_css'] = $extra_css;
        }
        if (isset($extra_js)) {
            $data['extra_js'] = $extra_js;
        }
        $template = $this->CI->load->view("templates/$template", $data, TRUE);
        return $template;
    }
}

Then you can $CI->load everything you want inside your library, models, other libraries, whatever.
See Utilize CI resource within your library for a thorough explanation of that. The, you call your library the usual way, $this->load->library('render') and then $this->render->whatever();

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文