2011-02-01 84 views
0

它来到我的注意,我目前在我的控制我的图像处理代码会更适合在模型模式,但我不知道,甚至从哪里开始这样做。重构控制器在代码点火器

我有一个控制器,它处理的图片上传,重命名文件,并使用其存储在数据库学说:

<?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模型的经验。

感谢所有帮助提前

回答

0

我对我自己的项目非常类似的问题:在控制器重复。我认为在你的情况下,只将该逻辑的一部分移入模型是有意义的,因为大部分逻辑在控制器中是有意义的。

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

然后您将仍然有一些重叠,但我看到的,因为控制器逻辑和模型逻辑在这种情况下是如此交织没有别的办法。