在代码点火器中将控制器重构为模型

时间:2011-02-01 15:05:48

标签: php codeigniter

我注意到我的控制器中目前的图像处理代码更适合模型,但我不确定从哪里开始这样做。

我有一个控制器,用于处理上传图像,重命名文件并使用Doctrine将其存储在数据库中:

<?php
class Addimage extends Controller 
{

    function index()
    {   


        $vars['content_view'] = 'uploadimage';
        $this->load->view('template', $vars);           

    }

    public function do_upload()
    {
        $this->load->library('form_validation');
        if($this->_submit_validate() == FALSE)
        {
/*THIS CODE BLOCK IS DUPLICATED FROM MY HOME PAGE CONTROLLER - this is one of the reasons I want to refactor.*/
            $vars['recentimages'] = Doctrine_Query::create()
        ->select('photo_path')
        ->from('Gif g')
        ->orderBy('g.created_at DESC')
        ->limit(12)
        ->execute();



        $vars['title'] = 'Home';
        $vars['content_view'] = 'welcome_message';


        $this->load->view('template_front', $vars);

        }
        else
        {           

            $basedir = $this->config->item('server_root') . $this->config->item('upload_dir');

            //If the directory doesn't already exist, create it.
            if (!is_dir($basedir))
            {
                mkdir($basedir, 0777);
            }           



            $config = array(
                'allowed_types' => "gif",           
                'upload_path' => $basedir,
                'remove_spaces' => true
            );
            $this->load->library('upload', $config);
            if(!$this->upload->do_upload())
            {
                $data['error'] = 'There was a problem with the upload';
            }
            else
            {

                $image_data = $this->upload->data();                
                $fileName = $image_data['file_name'];
                $title = $this->input->post('title');

                //Rename File based on how many of that letter
                //are already in the database
                $imageCount = Doctrine_Query::create()
                    ->select('COUNT(i.id) as num_images')
                    ->from('Gif i')                 
                    ->execute();

                $imageCount = $imageCount[0]->num_images++;
                //Rename file based on title and number of images in db. 
                $newFileName = preg_replace('/[^a-zA-Z0-9\s]/', '', $title) . '_' . $imageCount . $image_data['file_ext'];
                rename($basedir . $fileName, $basedir . $newFileName);


                $gif = new Gif();
                $gif->photo_path = $newFileName;
                $gif->title = $title;
                if(Current_User::user())
                {
                    $gif->User = Current_User::user();              
                }
                else
                {                   
                    $gif->User =  Doctrine::getTable('User')->findOneById($this->config->item('anonuid'));
                }
                $gif->save();
            }



            redirect('/', 'location');
        }
    }

    private function _submit_validate()
    {
        $this->form_validation->set_rules('title', 'Title', 'required');
        return $this->form_validation->run();
    }

}

我希望能够在模型中使用大部分内容,因为我正在使用模板系统来查看我的uploadimage.php视图只是上传表单的视图,以便可以将其放在任何页面上。另外,我只有使用Doctrine模型的经验。

感谢您提前提供任何帮助

1 个答案:

答案 0 :(得分:0)

我在自己的项目中有一个非常类似的问题:控制器中的重复。我认为在你的情况下,仅将该逻辑的一部分移动到模型中是有意义的,因为大多数逻辑在控制器中实际上是有意义的。

渲染视图肯定应该在控制器中,并且输入验证也是如此。我会将事务部分移动到模型:SQL,文件处理和图像处理。

然后你仍然会有一些重复但我没有别的办法,因为在这种情况下控制器逻辑和模型逻辑交织在一起。