Skip to content

Instantly share code, notes, and snippets.

@vladyslavstartsev
Last active January 16, 2019 16:44
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save vladyslavstartsev/498e3e5ba92b4f44c69bc8671de99a3c to your computer and use it in GitHub Desktop.
Save vladyslavstartsev/498e3e5ba92b4f44c69bc8671de99a3c to your computer and use it in GitHub Desktop.
laravel facades discussion

Found an example there is a thing in laravel called FormRequest. Sometimes, I need extra validation. So I need to use some service there somehow. What options I have

  1. facade
  2. service locator via app() global function
  3. I also have instance of container there via $this->container

which one is the best here? I suppose instance of container

I can't do constructor injection. Why? Because for me, it seams that there are too many arguments on the __construct method it look like this image

Am I wrong with this one?

Also they (FormRequests) help me to make controllers slim. I get all the extra data there (for example get user by email, I mean not the current session user, but the one that I, for example, edit) so I don't need to do this in other place.

what I also can do is to make some separate method, name it, for example, afterValidationRequestDataSetter, on the form request, call it at the end of all operations like $this->container->call([$this, 'afterValidationRequestDataSetter']) and it will inject all the dependencies that I need (I suppose, have not tested yet), but that

  1. is not constructor injection

  2. looks a bit like a magic since you can't do search on who calls this method (especially, it will be hard for new people on the project)

what is your ides about that?

p.s. not laravel zombie-fan and not forcing people to use facades

@vladyslavstartsev
Copy link
Author

@deleugpn Hi! Thanks for your message

you want to extract a complex validation rule out of the Form Request, correct?

Sometimes yes, but sometimes

Also they (FormRequests) help me to make controllers slim. I get all the extra data there

see getValidatorInstance method

I do need validation, but only one time, I mean I don't want to create separate class for it

Laravel real time Facades are GREAT

Sorry, but real time Facades, as I think, is the worst thing ever happened. You can catch all types of run time exceptions with no idea on how to debug it.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment