I have a lot of regex functions that validates the data.
They have a common domain. Where to place them, in service, helper wherelse? As I know to place it in controller is not good idea, because it is not reusable.
CodePudding user response:
You can write the validation regex to Models to make reusable. The detail info can be found in Validation - Laravel - The PHP Framework For Web Artisans
CodePudding user response:
Regex are more close to utility functions. In fact, utilities methods can be used anywhere depending upon the requirements. Therefor, these must be independent to any entity/models, controller or any class in Laravel.
Further, your question is closely connected with software engineering concept known as Coupling vs Cohesion
Coupling is defined as the degree of interdependence between the modules and Cohesion is defined as the degree of relationship between elements of the same module.
So a good software design says that there must always be low coupling and high cohesion.
Therefor, I believe if we create Regex utility module then it should work independently of any module/class/model/controller. They must have least dependency between each other to have low coupling as much as possible. Chances are higher that methods you define in Regex module will reuse across the other controllers/classes.
On the other hand, if you define Regex related methods inside controller or a model, then their inter module dependency will increase and chances are we will not be able to reuse these regex related methods anymore.
Therefor, a good idea is to isolate modules from each other that have no particular dependency and will reuse across the other modules.
In Laravel, we generally create Helper class/methods to achieve this. So you can defiantly go with Helper classes approach. Create a Regex Helper class and reuse it in whole application.
Cheers :)