Refactoring Controller to Model in Code Igniter

2019-06-14 13:07发布

问题:

It's come to my attention that my image processing code that I currently have in my controller would be better suited in a model, but I'm not sure even where to start to do this.

I have a controller that handles uploading an image, renaming the file and storing it in the database using 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();
    }

}

I would like to be able to have most of this in a model, because I'm using a template system for the views where my uploadimage.php view is just the upload form so that it can be dropped on any page. Also, I only have experience using Doctrine models.

Thanks for any help in advance

回答1:

I had a very similar issue on my own project: duplication in the controllers. I think in your case it makes sense to only move parts of that logic into the model, because most of it actually makes sense to be in a controller.

Rendering view definitely should be in a controller, and input validation as well. I would move the transactional part to the model: the SQL, file handling and image manipulation.

You will then still have some duplication but I see no other way since controller logic and model logic are so interwoven in this case.