2016-04-03 16 views
0

Ben bir laravel todo uygulaması oluşturuyorum. Kontrol cihazımda farklı yöntemler var ama içindeki tüm kodlar hemen hemen aynı. Tamamlanmamış yöntem ve tamamlanmış yöntemde, burada madde 1 farklıdır. Bunun dışında tüm kodlar aynıdır. Kod çoğaltmasını nasıl önleyebilirim?php kodu çoğaltması nasıl engellenir burada

public function all() 
{ 
    $user_id = $this->user_id; 

    $todos = $this->todos 
      ->where('user_id', $user_id) 
      ->orderBy('id', 'DESC')->paginate(15); 

    return view('todos.index', compact('todos')); 
} 


public function notCompleted() 
{ 
    $user_id = $this->user_id; 

    $todos = $this->todos 
      ->where('user_id', $user_id) 
      ->where('completed', false) 
      ->orderBy('id', 'DESC')->paginate(15); 

    return view('todos.index', compact('todos')); 
} 


public function completed() 
{ 
    $user_id = $this->user_id; 

    $todos = $this->todos 
      ->where('user_id', $user_id) 
      ->where('completed', true) 
      ->orderBy('id', 'DESC')->paginate(15); 
    return view('todos.index', compact('todos'));   
} 

cevap

1

Üç farklı yönteme ihtiyacım vardı, Bu yüzden bu yöntemleri sakladım ve kodları tek bir yöntemde ayıkladım. Ve bu kaydetme bir kod çoğaltma olabilir. Öyle değil mi?

public function all() 
{ 
    return $this->todoStatus('all'); 
} 

public function index() 
{ 
    return $this->todoStatus('current', false); 
} 

public function completed() 
{ 
    return $this->todoStatus('completed', true); 
} 

protected function todoStatus($completed, $status = false) 
{ 
    $user_id = $this->user_id; 

    if($completed === 'all') { 
     $todos = $this->todos 
      ->where('user_id', $user_id) 
      ->orderBy('id', 'DESC')->paginate(15); 
     return view('todos.index', compact('todos')); 
    } else { 
     $todos = $this->todos 
      ->where('user_id', $user_id) 
      ->where('completed', $status) 
      ->orderBy('id', 'DESC')->paginate(15); 
     return view('todos.index', compact('todos'));  
    } 
} 
0

Modelinizde, çoğaltmayı ortadan kaldırmak için bir kapsam tanımlayabilirsiniz.

public function scopeUserTodo($query, $userId){ 
    return $query->where('user_id', $userId); 
} 

public function scopeCompleted($query, $flag){ 
    return $query->where('completed', $flag); 
} 

public function scopeLatest($query){ 
    return $query->orderBy('id','Desc'); 
} 

Todo Modeli'nde örnek

Sonra denetleyicisi size daha ileri düzeyde denetleyici ve ismini todoState bir işlev oluşturabilir

public function all() 
{ 
    $user_id = $this->user_id; 

    $todos = $this->todos->userTodo($user_id)->latest()->paginate(15) 

    return view('todos.index', compact('todos')); 
} 


public function notCompleted() 
{ 
    $user_id = $this->user_id; 

    $todos = $this->todos->userTodo($user_id)->completed(false) 
      ->latest()->paginate(15) 


    return view('todos.index', compact('todos')); 
} 


public function completed() 
{ 
    $user_id = $this->user_id; 

     $todos = $this->todos->userTodo($user_id)->completed(true) 
      ->latest()->paginate(15) 

    return view('todos.index', compact('todos'));   
} 

için sorguları değiştirebilir ve ortak kod taşıyabilirsiniz orada tamamlanmamış ve tamamlanmış işlev arasında. Örnek

public function todoState($userId, $completed){ 

    $todos = $this->todos->userTodo($userId) 
       ->completed($completed) 
       ->latest() 
       ->paginate(15); 

    return $todos; 

} 
0
public function all($check, $value) 
{ 
    $user_id = $this->user_id; 

    if($check !== "completed"){ 
    $todos = $this->todos 
      ->where('user_id', $user_id) 
      ->where('completed', $value) 
      ->orderBy('id', 'DESC')->paginate(15); 

    return view('todos.index', compact('todos')); 
    }else{ 
     $todos = $this->todos 
      ->where('user_id', $user_id) 
      ->orderBy('id', 'DESC')->paginate(15); 

      return view('todos.index', compact('todos')); 
    } 
} 

böyle bir şey? ve sadece

+0

Bunu yapmış olabilir ama üç rotalar için üç ayrı yöntem gerek bana :) cevap herkese

ve teşekkürler. –

0

Sen kaç değiştirilebilir değişkenleri alır basit bir fonksiyon oluşturabilir işlev çağrısında veri geçmesine

şöyle bir şey: Denetleyiciniz kodunda Sonra

private function helper($userid, $completed) 
{ 
    return $this->todos 
     ->where('user_id', $userid) 
     ->where('completed', $completed) 
     ->orderBy('id', 'DESC')->paginate(15); 
} 

:

public function notCompleted() 
{ 
    $user_id = $this->user_id; 

    $todos = $this->helper($user_id, false); 

    return view('todos.index', compact('todos')); 
} 
+0

cevabı için teşekkürler, bunu yapabilirdim ama üç yol için üç ayrı yönteme ihtiyacım var. Yanıt için teşekkürler: –

+0

Sadece her rotadaki kod miktarını azaltmak istediğinizi düşündüm, sadece bir örnek verdim. Ben kod çoğaltma miktarını azaltmak için 3 rotanın her birinden yardımcısı çağıracağınız anlamına geliyordu, sadece 3 ile değiştirmemesi gerekiyordu :) net olmamak için üzgünüm :) – Graeme

0

GetByCompletedStatus için bir işlev oluştur ...

public function getByCompletedStatus($status) 
{ 
    $user_id = $this->user_id; 

    $todos = $this->todos 
     ->where('user_id', $user_id) 
     ->where('completed', $status) 
     ->orderBy('id', 'DESC')->paginate(15); 
    return view('todos.index', compact('todos')); 
} 

Ardından, gerektiği şekilde doğru veya yanlış iletir ve kod çoğaltmasından kaçınırsınız.

+0

Bunu yapabilirdim ama üç ayrı ihtiyacım var üç yol için yöntemler. Yanıt için teşekkürler –