2011-02-01 33 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,文件處理和圖像處理。

然後您將仍然有一些重疊,但我看到的,因爲控制器邏輯和模型邏輯在這種情況下是如此交織沒有別的辦法。