【问题标题】:How to get data in function extend controller?如何在功能扩展控制器中获取数据?
【发布时间】:2016-07-04 01:11:33
【问题描述】:

我的控制器是这样的:

<?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控制器中好像不能调用get_notification函数

我输入了函数get_notification(EX Controller),以便在所有控制器中读取

有什么办法可以解决我的问题吗?

【问题讨论】:

  • $data 定义在哪里?
  • @meda,不是$data,而是$this-&gt;data。我想在function get_notification (EX_Controller) 中获得$this-&gt;data['notification']$this-&gt;data['count_notification']。然后将其发送到 index_view。
  • 是的,但我看不出您在代码中的哪个位置声明了该属性,它也应该是公开的public $data;
  • @meda,我在EX_Controller 中添加public $data;,如下所示:&lt;?php class EX_Controller extends CI_Controller { public $data; public function __construct() { ...。但是,它不起作用
  • @moses_toh 在你的代码中哪里调用 get_notification

标签: php codeigniter


【解决方案1】:

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

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

【讨论】:

  • 我尝试不使用public $data;。它正在工作。所以,我就用这个:$this-&gt;load-&gt;model('notification_model'); $this-&gt;get_notification();
  • 就用这个:$this-&gt;load-&gt;model('notification_model'); $this-&gt;get_notification();。没有$data,它可以工作。我已经更新了你的答案
  • 我需要你的帮助。看这里:stackoverflow.com/questions/38189488/…
猜你喜欢
  • 2017-11-19
  • 1970-01-01
  • 2016-04-02
  • 2021-11-20
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2018-02-12
  • 1970-01-01
相关资源
最近更新 更多