CodeIgniter中重定向加载视图及全局导航栏数据传递问题
嘿,我来给你分享几个在CodeIgniter里解决这个需求的靠谱方案——既要搞定首页的分页数据传递,又能让所有页面都自动给导航栏带上通知数据,不用重复造轮子:
1. 用基础控制器抽离公共逻辑
既然所有页面都需要处理通知数据,最省心的方式是把这部分逻辑放到基础控制器里,让所有页面控制器都继承它,这样就不用在每个方法里重复写通知获取的代码了。
首先在application/core目录下创建MY_Controller.php(CodeIgniter默认会加载这个自定义基类):
class MY_Controller extends CI_Controller { public function __construct() { parent::__construct(); // 仅给登录用户加载通知 if ($this->session->userdata('logged_in')) { $this->load->model('Notification_model'); // 获取当前用户的未读通知(可以根据需求调整逻辑,比如限制数量) $unread_notifications = $this->Notification_model->get_unread_notifications( $this->session->userdata('user_id') ); // 用load->vars把通知数据全局传递给所有视图 $this->load->vars([ 'unread_notifications' => $unread_notifications, 'notification_count' => count($unread_notifications) ]); } } }
然后让你的Pages控制器继承这个基类:
class Pages extends MY_Controller { public function home() { // 处理首页的分页数据 $this->load->model('YourDataModel'); $this->load->library('pagination'); // 分页配置 $config['base_url'] = base_url('pages/home'); $config['total_rows'] = $this->YourDataModel->get_total_records(); $config['per_page'] = 10; $this->pagination->initialize($config); // 获取分页数据 $page = $this->uri->segment(3) ?? 0; $data['paginated_list'] = $this->YourDataModel->get_paginated_data( $config['per_page'], $page ); // 首页需要的额外数据 $data['extra_home_data'] = $this->YourDataModel->get_extra_info(); // 加载视图,通知数据已经通过基类全局传递,不用再传 $this->load->view('templates/header', $data); $this->load->view('pages/home'); $this->load->view('templates/footer'); } public function login() { // 登录页面逻辑 if ($this->input->post()) { // 验证登录信息... if ($login_valid) { $this->session->set_userdata([ 'logged_in' => true, 'user_id' => $user_id ]); redirect('pages/home'); } } $this->load->view('templates/header'); $this->load->view('pages/login'); $this->load->view('templates/footer'); } }
2. 导航栏视图的通知渲染
在你的导航栏模板(比如templates/header.php)里,直接用全局传递的通知数据渲染下拉菜单:
<nav class="navbar"> <!-- 其他导航链接 --> <?php if($this->session->userdata('logged_in')): ?> <div class="notification-dropdown"> <button class="notification-btn"> 通知 <span class="badge"><?= $notification_count ?></span> </button> <div class="dropdown-menu"> <?php if(empty($unread_notifications)): ?> <p class="empty-notice">暂无未读通知</p> <?php else: ?> <?php foreach($unread_notifications as $note): ?> <a href="<?= base_url('notifications/view/'.$note->id) ?>" class="notification-item"> <h5><?= $note->title ?></h5> <p><?= substr($note->content, 0, 50) ?>...</p> </a> <?php endforeach; ?> <?php endif; ?> </div> </div> <?php endif; ?> </nav>
3. 通知模型的核心逻辑
最后实现Notification_model来处理通知数据的查询:
class Notification_model extends CI_Model { public function get_unread_notifications($user_id) { return $this->db->where('user_id', $user_id) ->where('is_read', 0) ->order_by('created_at', 'DESC') ->limit(10) // 限制显示最新的10条 ->get('notifications') ->result(); } }
几个实用小贴士
- 分页逻辑和通知逻辑完全解耦,互不影响,首页该传的分页数据、额外数据正常传就行。
- 如果需要标记通知为已读,可以在用户点击通知时,在对应的控制器方法里调用模型的更新方法。
- 要是有部分页面不需要通知,也可以在子类控制器里重写
__construct方法,或者加判断跳过通知加载。
内容的提问来源于stack exchange,提问作者Chief Makii
相关产品推荐
相关产品推荐

