关于 CI 的简单问题。
我有一个带有表单、多个文本输入字段和文件上传的 View 。 我希望能够从文本字段中获取输入,将其保存到数据库,然后上传图像。
我通过将上传代码放在 Controller 中来实现此目的,如果上传成功,则会调用我的模型来更新数据库。
这是“最佳实践”,还是一种可接受的做法?或者应该将文件上传放入模型中。这有关系吗?
基本上我的代码是:
function edit_category()
{
$config['upload_path'] = 'images/category/';
$config['allowed_types'] = 'gif|jpg|jpeg|png';
$config['max_size'] = '1000';
$config['max_width'] = '300';
$config['max_height'] = '300';
$this->load->library('upload', $config);
if(!$this->upload->do_upload())
{
$this->session->set_flashdata('status', $this->upload->display_errors());
redirect('admin/category/edit/'.$this->input->post('catID'), 'location');
}
else /*no errors, upload is successful..*/
{
$fInfo = $this->upload->data();
//$this->_createThumbnail($fInfo['file_name']);
//process form POST data.
$data = array(
'catName' => $this->input->post('catName'),
'catDesc' => $this->input->post('catDesc'),
'catImage' => $fInfo['file_name']
);
/* update the database */
$category = $this->category_model->edit_category($data, $this->input->post('catID'));
最佳答案
我会将其放入模型中,因为我喜欢让 Controller 尽可能纤薄。我认为 Controller 是 View 和后台处理之间的链接,而不是处理本身。 我不确定这是否是“最佳实践”。按照您的方式,它肯定也会起作用。 CodeIgniter 允许您非常灵活地应用 mvc 理论。
关于file - 在 codeigniter 中使用上传类 - 模型还是 Controller ?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3263452/