Dynamic dependency injection for interface, based on user input

Finally after some days researching and thinking alot about the best approach for this, using Laravel I finally solved.

I have to say that this was specially difficult in Laravel 5.2, because in this version the Session middleware only is executed in the controllers used in a route, it means that if for some reason I used a controller (not linked for a rote) and try to get access to the session it is not going to be possible.

So, because I can not use the session I decided to use URL parameters, here you have the solution approach, I hope some of you found it useful.

so, you have an interface:

interface Service
{
    public function execute();
}

Then a couple of implementations for the interface:

The service one:

class ServiceOne implements Service
{
    public function execute()
    {
        .......
    }
}

The service two.

class ServiceTwo implements Service
{
    public function execute()
    {
        .......
    }
}

Now the interesting part: have a controller with a function that have a dependency with the Service interface BUT I need to resolve it dinamically to ServiceOne or ServiceTwo based in a use input. So:

The controller

class MyController extends Controller
{
    public function index(Service $service, ServiceRequest $request)
    {
        $service->execute();
        .......
    }
}

Please note that ServiceRequest, validated that the request already have the parameter that we need to resolve the dependency (call it 'service_name')

Now, in the AppServiceProvider we can resolve the dependency in this way:

class AppServiceProvider extends ServiceProvider
{
    public function boot()
    {
    }
    public function register()
    {
        //This specific dependency is going to be resolved only if
        //the request has the service_name field stablished
        if(Request::has('service_name'))
        {
            //Obtaining the name of the service to be used (class name)
            $className = $this->resolveClassName(Request::get('service_name')));
            $this->app->bind('Including\The\Namespace\For\Service', $className);
        }
    }
    protected function resolveClassName($className)
    {
        $resolver = new Resolver($className);
        $className = $resolver->resolveDependencyName();
        return $className;
    }
}

So now all the responsibilty is for the Resolver class, this class basically use the parameter passed to the contructor to return the fullname (with namespace) of the class that is going to be used as a implementation of the Service interface:

class Resolver
{
    protected $name;
    public function __construct($className)
    {
        $this->name = $className;
    }
    public function resolveDependencyName()
    {
        //This is just an example, you can use whatever as 'service_one'
        if($this->name === 'service_one')
        {
            return Full\Namespace\For\Class\Implementation\ServiceOne::class;
        }
        if($this->name === 'service_two')
        {
            return Full\Namespace\For\Class\Implementation\ServiceTwo::class;
        }
        //If none, so whrow an exception because the dependency can not be resolved
        throw new ResolverException;
    }
}