如何在函数中获取数据扩展控制器?

时间:2016-07-04 01:11:34

标签: php codeigniter

我的控制器是这样的:

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

class Dashboard extends EX_Controller
{
    public function __construct()
    {
        parent::__construct();
    }

    public function index()
    {
        $this->data['news'] = $this->dashboard_model->get_news();
        $this->load->view('dashboard/index_view', $this->data);
    }

我的EX_Controller是这样的:

<?php
class EX_Controller extends CI_Controller
{
    public $data;
    public function __construct()
    {
        parent::__construct();

        $this->load->model('notification_model');
        $this->get_notification();
    }

    public function get_notification()
    {
        $session = $this->session->userdata('login');
        $this->data['notification'] = $this->notification_model->get($session);
        $this->data['count_notification'] = $this->notification_model->get_count_notification($session['customer_id']);
    }
}
?>

我的索引视图是这样的:

<?php
    foreach ($notification as $value) {
?>
        <li>
        <?php
            echo '<a href='.base_url().'notification/notif/'.$value['notification_id'].'>';
        ?>
                <span class="time">
                <?php 
                    echo time_elapsed($value['notification_created_date']); 
                ?>
                </span>
                <span class="details">
                    <span class="label label-sm label-icon label-info">
                        <i class="fa fa-bookmark"></i>
                    </span> <?php echo $value['notification_message']; ?>
                </span>
            </a>
        </li>
<?php
    }
?> 

执行时,存在错误:

Message:  Undefined variable: count_notification
Message:  Undefined variable: notification

似乎无法在EX Controller中调用get_notification函数

我将函数get_notification(EX Controller)放入所有控制器中读取

解决我问题的任何解决方案?

1 个答案:

答案 0 :(得分:1)

解决这个问题的方法就是使用它:

$this->load->model('notification_model'); 
$this->get_notification();