F
F
frytempa2019-02-16 20:54:13
Laravel
frytempa, 2019-02-16 20:54:13

How to throw the necessary classes through the container?

There is a method in the controller, 2 parameters come from the front, for example, type and param.
There is a service in which, depending on these two parameters, you need to throw the necessary interface implementations into the constructor.

class Controller
{
    public function index(Request $request, Service $service)
    {
        return $service->run();
    }
}

class Service
{
    protected $typeClass;
    protected $paramClass;
    public function __constructor(IType, $typeClass, IParam $paramClass)
    {
        $this->typeClass = $typeClass;
         $this->paramClass = $paramClass;
    }
}

Currently used like this
public function index(Request $request, ServiceBuilder $builder)
    {
        $service = $builder->get($request->get('type'), $request->get('param'));

        return $service->run()
    }

Is there a better solution without using the locator service?

Answer the question

In order to leave comments, you need to log in

1 answer(s)
A
Alexander Pushkarev, 2019-02-17
@AXP-dev

Is this method not suitable?

class Controller
{
    public function index(Request $request)
    {
        return (new Service($request->get('type'), $request->get('param')))->run();
    }
}

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question