2016-04-03 77 views
0

我正在创建一个laravel待办事项应用程序。在我的控制器中有不同的方法,但其中的所有代码几乎相同。在notCompleted方法和完成的方法中,还有1个不同的where子句。除了所有的代码是相同的。我该如何避免代码重复?如何避免这里的PHP代码重复

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'));   
} 

回答

1

我需要三种不同的方法,所以我保留这些方法并将代码提取到一个方法中。这可能会节省代码重复。不是吗?

,并感谢所有谁回应我:)

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

在您的模型中,您可以定义一个范围来消除重复。例如

在藤型号

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'); 
} 

然后在你的控制器,你可以改变你的查询

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'));   
} 

可以进一步建立在你的控制器,并将其命名todoState功能和移动通用代码在那里不完成和完成的功能之间。例如

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')); 
    } 
} 

这样的事情?而只是通过在函数调用数据

+0

我可以这样做,但我需要三个途径三种不同的方法。感谢您的回应 –

0

你可以创建一个简单的函数,它在几个多变变量

所以像:

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

我可以做到这一点,但我需要三种路线的三种独立的方法。感谢您的回复 –

+0

我以为你只是想减少每条路线的代码量,我只举了一个例子。我的意思是你可以从你的3条路线中的每条路径中调用helper来减少代码重复的数量,而不是用所有的3代替所有的3 :)对不明确:) – Graeme

0

创建一个函数getByCompletedStatus ...

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')); 
} 

然后您将根据需要传递true或false,并避免代码重复。

+0

我本来可以做到这一点,但我需要三个单独的三条路线的方法。 感谢您的回复 –