Answer the question
In order to leave comments, you need to log in
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;
}
}
public function index(Request $request, ServiceBuilder $builder)
{
$service = $builder->get($request->get('type'), $request->get('param'));
return $service->run()
}
Answer the question
In order to leave comments, you need to log in
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 questionAsk a Question
731 491 924 answers to any question