简体   繁体   English

codeigniter - >加载多个库/类时遇到问题

[英]codeigniter -> having trouble loading multiple libraries/classes

Ok, so in my base controller (page.php) I have the following code which works fine: 好的,所以在我的基本控制器(page.php)中我有以下代码可以正常工作:

   $this->load->library('Siteclass');
   $mysite = new site_model();

The siteclass library references a model named site_model and instantiates based on data received from that model. siteclass库引用名为site_model的模型,并根据从该模型接收的数据进行实例化。 All is good. 一切都很好。

Now I want to load another library so that I can instantiate another object as well. 现在,我想加载另一个库,以便可以实例化另一个对象。 So I add this to page.php: 所以我将其添加到page.php:

 $this->load->library('Memberclass');
 $mysite = new member_model();

But now I get the following error: 但是现在我得到以下错误:

 Message: Undefined property: Memberclass::$site_model
 Filename: libraries/Loader.php
 Line Number: 1035

From what I can tell, it seems that the loader class, when being applied to the Memberclass, is somehow still referencing the site_model instead of the member_model. 据我所知,似乎加载器类在应用于Memberclass时,仍以某种方式引用site_model而不是member_model。 I've checked my code and I am definitely calling the correct files. 我检查了我的代码,我肯定是在调用正确的文件。

Here's what Siteclass.php looks like: 这是Siteclass.php的样子:

 if ( ! defined('BASEPATH')) exit('No direct script access allowed'); 

 class Siteclass extends Controller {
    function __construct() {
        parent::Controller();
        $this->load->model('Site_model');
        $data = $this->Site_model->load_site_data();
        // etc etc

and here's what Memberclass.php looks like: 这是Memberclass.php的样子:

if ( ! defined('BASEPATH')) exit('No direct script access allowed'); 

class Memberclass extends Controller {
function __construct() {
        parent::Controller();
    $this->load->model('Member_model');
        $data = $this->Member_model->load_member_data();
        // etc etc

Thanks in advance for any help! 在此先感谢您的帮助!

Gary 加里

I think you're confused about how MVC works in CodeIgniter. 我认为您对MVC在CodeIgniter中的工作方式感到困惑。 Why are you using the loader class to create a controller? 为什么要使用loader类创建控制器? Why are you creating a stand-alone instance of your model outside of your controller class? 为什么要在控制器类之外创建模型的独立实例?

In CodeIgniter, your URLs represent paths to your controllers' methods. 在CodeIgniter中,您的URL表示控制器方法的路径。 That means that your "base controller" should automatically be instantiated if you go to: 这意味着如果你去,你的“基础控制器”应该自动实例化:

www.example.com/memberclass www.example.com/memberclass

Or perhaps more to the point, if you have a link like this: 或者更重要的是,如果您有这样的链接:

www.example.com/page www.example.com/page

You should have a file in your /application/controllers directory called page.php which looks like this: 您应该在/application/controllers目录中有一个名为page.php ,如下所示:

if ( ! defined('BASEPATH')) exit('No direct script access allowed'); 

class Page extends Controller {
    function __construct() {
        parent::Controller();
        // etc etc

Furthermore, unless you're loading data from your model to be used every single time you call this controller , you'll want to put your model calls inside a non-constructor method of this class. 此外,除非您每次调用此控制器时从模型中加载要使用的数据,否则您需要将模型调用放在此类的非构造函数方法中。 Something like: 就像是:

class Page extends Controller {
    function __construct() {
        parent::Controller();
    }

    function index() {
        $this->load->model('Member_model');
        $data = $this->Member_model->load_member_data();
        $this->load->view('myview', array('data'=>$data));
    }
}

So again...not entirely sure what context you're doing this all in, but it seems like you're not standing firmly within the framework. 所以再次......不完全确定你在做什么背景,但看起来你并没有坚定地站在框架内。 There's basically no reason you should be using the loader class to load controllers, and furthermore there's no reason you should be creating stand-alone instances of model classes using PHP's new keyword. 基本上没有理由你应该使用loader类加载控制器,而且你没有理由使用PHP的new关键字创建模型类的独立实例。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM