CI3框架中如何在控制器的两个函数间传递指定数据?
在CodeIgniter 3中实现控制器内方法间的数据传递
问题描述
我在CodeIgniter 3框架中编写了如下Infograph控制器:
class Infograph extends CI_Controller { public function index() { $wilayah = $this->input->post('wilayah', TRUE); $kodeSubWilayah = $this->input->post('subWilayah', TRUE); if ($kodeSubWilayah) { $kodeSubWilayah = $this->encryption->decrypt($kodeSubWilayah); if ($kodeSubWilayah === false) { redirect('test/404'); } } $data = []; if ($wilayah) { $data['wilayah'] = strtolower($wilayah); } if ($kodeSubWilayah) { // 需先加载对应数据库连接 $db2 = $this->load->database('db2', TRUE); $data['subWilayah'] = $db2->query("SELECT * FROM provinsi WHERE id = ?", [$kodeSubWilayah])->result_array(); } } public function export() { // 需要获取$data['wilayah']和$data['subWilayah']数据 } }
其中index函数通过POST请求获取并处理得到$data['wilayah']和$data['subWilayah']数据,需要将这两个数据传递到同控制器的export函数中,该如何实现?
解决方案
方法一:使用Session存储临时数据
CodeIgniter的Session类可在请求间存储数据,适合从index跳转至export的场景:
- 加载Session库(可在
application/config/autoload.php中配置自动加载,或在控制器构造函数中手动加载):
public function __construct() { parent::__construct(); $this->load->library('session'); }
- 在
index方法中将数据存入Session:
public function index() { // ... 原有处理代码 ... if (isset($data['wilayah'])) { $this->session->set_userdata('wilayah', $data['wilayah']); } if (isset($data['subWilayah'])) { $this->session->set_userdata('subWilayah', $data['subWilayah']); } // 跳转到export方法触发导出 redirect('infograph/export'); }
- 在
export方法中读取并销毁临时数据:
public function export() { $wilayah = $this->session->userdata('wilayah'); $subWilayah = $this->session->userdata('subWilayah'); // 执行导出逻辑... // 用完后清理临时数据 $this->session->unset_userdata(['wilayah', 'subWilayah']); }
方法二:加密后通过URL参数传递
如果是用户主动点击导出按钮触发操作,可将关键数据加密后作为URL参数传递:
- 在
index方法中生成加密的导出链接(需传递到视图渲染):
public function index() { // ... 原有处理代码 ... $exportParams = []; if (isset($data['wilayah'])) { $exportParams['wilayah'] = $this->encryption->encrypt($data['wilayah']); } if (isset($data['subWilayah'])) { // 建议仅传递ID,避免传递大量数据,在export中重新查询 $exportParams['sub_id'] = $this->encryption->encrypt($data['subWilayah'][0]['id']); } $data['export_url'] = site_url('infograph/export?' . http_build_query($exportParams)); $this->load->view('infograph_view', $data); }
- 在
export方法中解密参数并处理:
public function export() { $wilayah = $this->input->get('wilayah', TRUE); $subId = $this->input->get('sub_id', TRUE); $data = []; if ($wilayah) { $wilayah = $this->encryption->decrypt($wilayah); $wilayah === false && redirect('test/404'); $data['wilayah'] = strtolower($wilayah); } if ($subId) { $subId = $this->encryption->decrypt($subId); $subId === false && redirect('test/404'); $db2 = $this->load->database('db2', TRUE); $data['subWilayah'] = $db2->query("SELECT * FROM provinsi WHERE id = ?", [$subId])->result_array(); } // 执行导出逻辑... }
方法三:直接调用export方法并传参
如果无需跳转,在index处理完数据后直接执行导出逻辑,可直接传递数组参数:
public function index() { // ... 原有处理代码 ... // 直接调用export方法并传递处理好的数据 $this->export($data); } public function export($data = []) { $wilayah = $data['wilayah'] ?? null; $subWilayah = $data['subWilayah'] ?? null; // 执行导出逻辑... }
内容的提问来源于stack exchange,提问作者Zombatar
相关产品推荐
相关产品推荐

