Laravel 5.1> use @inject in view to display content

I am working on a small web application that will have a personal messaging system. As long as the user subscribes to their received or written messages, it will appear in the toolbar area inside the dropdown menu. Nothing new: -)

The toolbar will be displayed most of the time while the user is navigating the page. So instead of injecting an inbox implementation into every controller that can be called via routes.php, I am thinking of using the new @inject blade function.

It will look like this (distracted)

  • Calling ControllerMethod on Toolbar via @inject
  • ControllerMethod queries injected by InterfaceMethod (based on constructor Injection)
  • Service Container Serves Implementation
  • ImplementationMethod returns incoming messages

But I'm not sure if this is a good design pattern because I'm relatively new to dependency injection in general. I would appreciate some problems.

+3


source to share


1 answer


There are two things I don't like.

1) Laravel's @Inject view directive does not actually do any "dependency injection", even though it indicates it. This is in fact using a service location, a pattern that is widely considered an anti-pattern.

Basically, via @Inject, the view has access to anything in the container. He can get whatever he wants. He is not told what should be. This is the fundamental difference between dependency injection and service location.



2) Injecting services directly into the view undoubtedly means that you are adding more complexity to your application because you have added more places where services can be injected into your templates. While this can make things faster and easier, short term, long term, it will be more difficult to work with.

3) How do you check how you are retrieving and manipulating data if this is happening in your view? You will probably have to use functional testing, which, while not so bad, still won't be as useful as unit testing for something like this.

I would recommend sticking with the way DI is usually done and actually using DI.

0


source







All Articles