(Laravel) Dynamic dependency injection for interface, based on user input
<p>I am currently facing a very interesting dilemma with my architecture and implementation.</p>

<p>I have an interface called <code>ServiceInterface</code> which have a method called <code>execute()</code></p>

<p>Then I have two different implementations for this interface: <code>Service1</code> and <code>Service2</code>, which implements the execute method properly.</p>

<p>I have a controller called <code>MainController</code> and this controller has a "type-hint" for the <code>ServiceInterface</code> (<strong>dependency injection</strong>), it means that both, <code>Service1</code> and <code>Service2</code>, can be called as resolution for that dependency injection.</p>

<p><strong>Now the fun part:</strong></p>

<p>I do not know which of those implementations to use (<code>Service1</code> or <code>Service2</code>) because I just know if I can use one or other <strong>based on a user input</strong> from a previous step.</p>

<p>It means the user choose a service and based on that value I know if a can use <code>Service1</code> or <code>Service2</code>.</p>

<p>I am currently solving the dependency injection using a session value, so depending of the value I return an instance or other, BUT I really think that it is not a good way to do it.</p>

<p>Please, let me know if you faced something similar and, how do you solve it, or what can I do to achieve this in the right way.</p>

<p>Thanks in advance. Please let me know if further information is required.</p>