file - 在 codeigniter 中使用上传类 - 模型还是 Controller ?

标签 file class codeigniter upload

关于 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/

相关文章:

arrays - 将文件中的行复制到 char *array[]?

php - Codeigniter – 如何避免 session 的数据库缓存

c++ - 如何让两个不同的对象方法互相用作参数

mysql join 2表选择一行字段有多行

php - 其余 API 更新

java - 创建新的 File 实例是否会导致创建空文件?

java - 在 Java 中高效地读取/写入键/值对到文件

python - 获取 Google App Engine 平台服务器文件夹中的静态文件列表

python - 如何在新类中实例化对象?

c++ - 派生类的新构造函数未构建 : "overloaded member function not found"