2015-05-12 60 views
2

下面是我的控制器Admin/MoviesController的'store'方法示例。它已經看起來很大,'更新'的方法會更大。Laravel 5 - 清潔代碼,在哪裏保持業務邏輯(控制器示例)

的algoritm是:在CreateMovieRequest

  1. 驗證請求數據,並與所有可填寫字段創建新的電影 。
  2. 上傳海報
  3. 填寫和保存所有重要的,但不是必需的字段(元標題,meta描述。)
  4. 然後4與解析代碼,重視電影流派,演員,導演,國家塊。
  5. 要求使用第三方API

我的問題IMDB的評級:

  • 我是不是應該把所有這些代碼模型,並將其劃分爲像小方法:removeGenres($ ID), addGenres(Request $ request),...
  • 是否有一些最佳實踐?我說的不是MVC,而是Laravel的特性。爲了保持幕後的邏輯,我只使用了請求驗證。

    public function store(CreateMovieRequest $request) { 
    
    $movie = Movies::create($request->except('poster')); 
    
    /* Uploading poster */ 
    if ($request->hasFile('poster')) { 
        $poster = \Image::make($request->file('poster')); 
    
        $poster->fit(250, 360, function ($constraint) { 
         $constraint->upsize(); 
        }); 
    
        $path = storage_path() . '/images/movies/'.$movie->id.'/'; 
    
        if(! \File::exists($path)) { 
         \File::makeDirectory($path); 
        } 
    
        $filename = time() . '.' . $request->file('poster')->getClientOriginalExtension(); 
        $poster->save($path . $filename); 
        $movie->poster = $filename; 
    } 
    
    /* If 'Meta Title' is empty, then fill it with the name of the movie */ 
    if (empty($movie->seo_title)) { 
        $movie->seo_title = $movie->title; 
    } 
    
    /* If 'Meta Description' is empty, then fill it with the description of the movie */ 
    if (empty($movie->seo_description)) { 
        $movie->seo_description = $movie->description; 
    } 
    
    // Apply all changes 
    $movie->save(); 
    
    /* Parsing comma separated string of genres 
    * and attaching them to movie */ 
    if (!empty($request->input('genres'))) { 
    
        $genres = explode(',', $request->input('genres')); 
    
        foreach($genres as $item) { 
         $name = mb_strtolower(trim($item), 'UTF-8'); 
    
         $genre = Genre::where('name', $name)->first(); 
    
         /* If such genre doesn't exists in 'genres' table 
         * then we create a new one */ 
         if (empty($genre)) { 
          $genre = new Genre(); 
          $genre->fill(['name' => $name])->save(); 
         } 
    
         $movie->genres()->attach($genre->id); 
        } 
    } 
    
    /* Parsing comma separated string of countries 
    * and attaching them to movie */ 
    if (!empty($request->input('countries'))) { 
        $countries = explode(',', $request->input('countries')); 
        foreach($countries as $item) { 
         $name = mb_strtolower(trim($item), 'UTF-8'); 
    
         $country = Country::where('name', $name)->first(); 
    
         if (empty($country)) { 
          $country = new Country(); 
          $country->fill(['name' => $name])->save(); 
         } 
    
         $movie->countries()->attach($country->id); 
        } 
    } 
    
    /* Parsing comma separated string of directors 
    * and attaching them to movie */ 
    if (!empty($request->input('directors'))) { 
        $directors = explode(',', $request->input('directors')); 
        foreach($directors as $item) { 
         $name = mb_strtolower(trim($item), 'UTF-8'); 
    
         // Actors and Directors stored in the same table 'actors' 
         $director = Actor::where('fullname', trim($name))->first(); 
    
         if (empty($director)) { 
          $director = new Actor(); 
          $director->fill(['fullname' => $name])->save(); 
         } 
         // Save this relation to 'movie_director' table 
         $movie->directors()->attach($director->id); 
        } 
    } 
    
    /* Parsing comma separated string of actors 
    * and attaching them to movie */ 
    if (!empty($request->input('actors'))) { 
    
        $actors = explode(',', $request->input('actors')); 
        foreach($actors as $item) { 
         $name = mb_strtolower(trim($item), 'UTF-8'); 
    
         $actor = Actor::where('fullname', $name)->first(); 
    
         if (empty($actor)) { 
          $actor = new Actor(); 
          $actor->fill(['fullname' => $name])->save(); 
         } 
    
         // Save this relation to 'movie_actor' table 
         $movie->actors()->attach($actor->id); 
        } 
    } 
    
    // Updating IMDB and Kinopoisk ratings 
    if (!empty($movie->kinopoisk_id)) { 
        $content = Curl::get('http://rating.kinopoisk.ru/'.$movie->kinopoisk_id.'.xml'); 
    
        $xml = new \SimpleXMLElement($content[0]->getContent()); 
    
        $movie->rating_kinopoisk = (double) $xml->kp_rating; 
        $movie->rating_imdb = (double) $xml->imdb_rating; 
        $movie->num_votes_kinopoisk = (int) $xml->kp_rating['num_vote']; 
        $movie->num_votes_imdb = (int) $xml->imdb_rating['num_vote']; 
    
        $movie->save(); 
    } 
    
    return redirect('/admin/movies'); 
    } 
    

回答

8

你需要考慮你如何能重複利用的代碼,如果你需要在其他類別或項目模塊使用它。對於啓動,你可以做這樣的事情:

電影模式,可以爲了改善到:

  • 管理的屬性是如何設置好的
  • 共創美好功能功能包括路/處理人際關係的數據

看看如何實現影片的功能:

class Movie{ 

    public function __construct(){ 

     //If 'Meta Title' is empty, then fill it with the name of the movie 
     $this->seo_title = empty($movie->seo_title) 
      ? $movie->title 
      : $otherValue; 

     //If 'Meta Description' is empty, 
     //then fill it with the description of the movie 
     $movie->seo_description = empty($movie->seo_description) 
      ? $movie->description 
      : $anotherValue; 

     $this->updateKinopoisk(); 
    } 

    /* 
    * Parsing comma separated string of countries and attaching them to movie 
    */ 
    public function attachCountries($countries){ 

     foreach($countries as $item) { 
      $name = mb_strtolower(trim($item), 'UTF-8'); 

      $country = Country::where('name', $name)->first(); 

      if (empty($country)) { 
       $country = new Country(); 
       $country->fill(['name' => $name])->save(); 
      } 

      $movie->countries()->attach($country->id); 
     } 
    } 

    /* 
    * Update Kinopoisk information 
    */ 
    public function updateKinopoisk(){} 

    /* 
    * Directors 
    */ 
    public function attachDirectors($directors){ ... } 

    /* 
     * Actores 
     */ 
     public function attachActors($actors){ ... } 

     /* 
     * Genders 
     */ 
     public function attachActors($actors){ ... } 
} 

海報,您可以considere使用服務提供商(我會告訴這個例子,因爲我不知道你的海報模式 的樣子):

public class PosterManager{ 

    public static function upload($file, $movie){ 
     $poster = \Image::make($file); 
     $poster->fit(250, 360, function ($constraint) { 
      $constraint->upsize(); 
     }); 

     $path = config('app.images') . $movie->id.'/'; 

     if(! \File::exists($path)) { 
      \File::makeDirectory($path); 
     } 

     $filename = time() . '.' . $file->getClientOriginalExtension(); 
     $poster->save($path . $filename); 

     return $poster; 
    } 
} 

配置文件 嘗試使用配置文件存儲相關的應用程序常量/數據,例如存儲電影圖像路徑:

'images' => storage_path() . '/images/movies/'; 

現在,您可以撥打01全球範圍內。如果你需要改變路徑,只需要設置配置文件。

作爲注入類的控制器。 最後,控制器被用作只需要注入代碼的類:

public function store(CreateMovieRequest $request) { 
    $movie = Movies::create($request->except('poster')); 

    /* Uploading poster */ 
    if ($request->hasFile('poster')) { 
     $file = $request->file('poster'); 
     $poster = \PosterManager::upload($file, $movie); 
     $movie->poster = $poster->filename; 
    } 

    if (!empty($request->input('genres'))) { 
     $genres = explode(',', $request->input('genres')); 

     $movie->attachGenders($genders); 
    } 

    // movie->attachDirectors(); 
    // movie->attachCountries(); 

    // Apply all changes 
    $movie->save(); 

    return redirect('/admin/movies'); 
} 
相關問題