我正在使用Code Igniter框架构建一个应用程序,我在其中构建了许多不同的库,我将这些库加载到需要它们的控制器中。
当我尝试将多个库加载到我的“home”控制器时,我收到以下消息:
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;
}
}
}
&GT;
答案 0 :(得分:1)
我设法通过不扩展CI_Controller来解决这个问题,如上所示,然后包括$ CI =&amp; get_instance();并使用它来访问CI库方法和模型。
答案 1 :(得分:0)
我在__construct
方法上加载多个库时面临同样的问题。
我通过单独加载它来处理这个问题。在需要它的特定Controller方法中加载每个。
示例:
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 );
}
}
现在正如预期的那样工作。
答案 2 :(得分:-1)
In controller add constructor method
public function __construct()
{
//Here you will load all libraries and models files
}