Best practice for init Valitron Validation library?

In general, a theoretical question, from the series "there are a bunch of solutions, what is the best."

Thinking about using some kind of library to validate data from forms. Externally, I liked the famous vlucas/valitron library

I want to create my own rule that looks to see if a unique email address has been submitted for registration.

The implementation of custom rules is as follows:

Valitron\Validator::addRule('alwaysFail', function($field, $value, array $params, array $fields) {
return false;
}, 'Everything you do is wrong. You fail.');

In other words, the static method is called, the rule is added on the fly to the static array, and it can be used below the code

Now about the main thing

For a long time I was wondering - where and how in the structure of micro frameworks should such initialization blocks of code be set here? … Indeed, in fact it’s just a section that extends the base library on the fly, and not as usual through inheritance. The main problem is that all this initialization must be called before any checks, it means “at the top” in the code tree.

What options do I have:

  1. Add the code to the index.php file, but this is some kind of trash on the senses.
  2. Use middleware
  3. Maybe some kind of service initialization file? But then how to use them if there will be a dozen such libraries? Obviously, the best way for today is composer, but it does autoload files on demand (if they use the code from there), or am I mistaken? Maybe there is some setting that allows the composer to specify initialization files?

And yet - do I understand correctly that such a structure as this validator is not entirely correct?
Still, the option of creating classes for each rule is somehow more familiar or something, or am I mistaken? …

Thank you!

  1. Putting everything into index.php is a mess.
  2. A middleware is only responsible for HTTP specific stuff (request, response, headers etc…) and belongs to the infrastructure. Don’t put domain specific logic into the middleware.
  3. A service class is the perfect place for your domain (business logic). You don’t need a library, it’s just a native PHP class with a specific class name and method. The controller / action invokes the service class to validate (and save) the input. Use namespaces to organize your classes under src/. Example