mercredi 27 novembre 2019

How to adapt Laravel controller code to follow DRY principle when there is overlap between assumptions?

My aim is to adapt the following controller methods to follow the DRY principle by moving the validation to form requests. However, I have a problem which is that there are overlaps between validation for different methods. For example, let's say that the input form has only one field and that's name, and that it's related to a Task model. Let's consider 3 controller methods now:

  • store method: validate that name isn't empty
  • update method: validate that name isn't empty and that a Task with the given $id exists
  • destroy method: validate that a Task with the given $id exists

So for the store method I am checking the first assumption, for the destroy method I am checking the second assumption, and for the update method I am checking both.

So, ideally, I would like to be able to do something like...

public function store(StoreTask $request)...
public function update(Store Task TaskExists $request, $id)...
public function destroy(TaskExists $id)...

...but I am very unclear on how to write the syntax for this, and whether there is some other approach which I'm missing to accomplish the same thing.



from Newest questions tagged laravel-5 - Stack Overflow https://ift.tt/34Buj64
via IFTTT

Aucun commentaire:

Enregistrer un commentaire