1

我正在使用 Code Igniter 框架构建一个应用程序,在该框架中我构建了许多不同的库,我将这些库加载到需要它们的控制器中。

当我尝试将多个库加载到我的“主”控制器中时,我收到以下消息:

A PHP Error was encountered

Severity: Notice

Message: Undefined property: Home::$messaging

Filename: controllers/home.php

Line Number: 73


Fatal error: Call to a member function getMessages() on a non-object in /home/totalcar/public_html/application/controllers/home.php on line 73

我以前从未遇到过这个错误,因为在加载多个库之前我有控制器没问题。似乎认为消息传递库不存在或未加载。我已经确保库的文件名有一个大写的第一个字母,类声明也是如此,这似乎没有任何区别。

我分别加载我的库,如下所示。(请注意,我已从控制器中删除了许多不相关的代码,以便在此处阅读)

public function index() {
        ...         

        //Load Helpers
        $this->load->helper('menu');
        $this->load->helper('layout');          

        //Load Libraries
        $this->load->library('pusher');         
        $this->load->library('test');           

        //Load User Details
        $data['userid'] = $this->session->userdata('userid');

        $data['modules'] = getModules($this->layout_model, $this->socialmedia_model);

        //Build Menu
        $data['menuItems'] = buildMenu($this->session->userdata('user_group_id'), $this->config);
        $data['firstName'] = $this->session->userdata('firstName');


        //Unread Messages 
        $options = array(
            'userId'  => $data['userid'],
            'opened' => 0
        );
        $this->load->library('messaging', $options);            
        $data['unreadMessages'] = $this->messaging->getMessages();
        $data['mailIcon'] = (empty($data['unreadMessages'])) ? "mailButton.png" : "mailButtonUnread.png";

有人可以在这里提供任何指导吗?谢谢

编辑:这是我的图书馆代码:

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

class Messaging extends CI_Controller {

    private $options;
    private $unreadMessages;
    public $mailIcon;

    function __construct($options) {
        parent::__construct();
        $this->load->model('messaging_model');
        $this->load->model('staff_model');
        $this->options = $options;
    }

    function getMessages() {
        $unreadMessages = $this->messaging_model->getMessages($this->options);
        $this->unreadMessagesReturn = array();
        $mailIcon = (empty($unreadMessages)) ? "mailButton.png" : "mailButtonUnread.png";
        if(!empty($unreadMessages)) {
            foreach($unreadMessages as $message) {
                $fromUserDetails = $this->staff_model->getStaffMemberDetails($message['from_id']);
                if((isset($fromUserDetails['profile_image_small'])) && $fromUserDetails['profile_image_small'] != "") {
                    $profileImageSmall = $this->config->base_url() . "images/profileImages/" . $fromUserDetails['profile_image_small']; 
                } else {
                    $profileImageSmall = $this->config->base_url() . "images/noImage.png";
                }
                $name = $fromUserDetails[0]['first_name'] . " " . $fromUserDetails[0]['last_name'];
                $dateSent = date('Y-m-d', strtotime($message['time_sent']));
                $messageTruncated = substr($message['message'], 0, 70) . "...";
                $this->unreadMessagesReturn[] = array(
                    'fromPic'          => $profileImageSmall,
                    'fromName'         => $name,
                    'subject'          => $message['subject'],
                    'message'          => $message['message'],
                    'messageTruncated' => $messageTruncated,
                    'dateSent'         => $dateSent
                );
            }
            return $this->unreadMessagesReturn;
        }
    }       
}

?>

4

3 回答 3

1

我设法通过不扩展 CI_Controller 来解决这个问题,如上所示,然后包括 $CI = & get_instance(); 行并使用它访问 CI 库方法和模型。

于 2013-10-07T09:49:00.393 回答
0

我在方法上加载多个库时遇到了同样的问题__construct

我通过单独加载它们来处理这个问题。在需要它的特定控制器方法中加载每一个。

例子:

class S extends CI_Controller {
   public function __construct() {
      // -- notice that there are not loadings here
      parent::__construct();
   }
   public function index() {       
      $this->load->view('vista_v');
   }
   public function registro( $accion = 'l', $parametros = '' ){
      $this->load->library('../controllers/analistas_c'); 
      $this->analistas_c->registro( $accion, $parametros );
   }
   public function tarea( $accion = 'l', $parametros = '' ){
      $this->load->library('../controllers/tareas_c');
      $this->tareas_c->registro( $accion, $parametros );
   }
}

这现在按预期工作。

于 2014-06-28T18:21:30.070 回答
-1
In controller add constructor method
public function __construct()
{
//Here you will load all libraries and models files
}
于 2013-10-04T08:44:51.610 回答