Zend framework2 zf2-将参数传递给服务管理器

Zend framework2 zf2-将参数传递给服务管理器,zend-framework2,Zend Framework2,我有一个工厂服务,其中我想从控制器发送额外的参数,下面是我目前的做法 $apiSuccess = new ApiSuccess(array( 'status' => 'success', 'code' => 'user_authenticate_success', 'href' => 'app/welcome', 'message' => 'User Authenticated Successfully' )); $apiResponse

我有一个工厂服务,其中我想从控制器发送额外的参数,下面是我目前的做法

$apiSuccess = new ApiSuccess(array(
    'status' => 'success',
    'code' => 'user_authenticate_success',
    'href' => 'app/welcome',
    'message' => 'User Authenticated Successfully'
));

$apiResponse = $this->getServiceLocator()->get('api_response');
$apiResponse->setHttpCode(200);
$apiResponse->setContent($apiSuccess);
$apiResponse->dispatch();
这是我的工厂服务课

class ApiJsonResponseFactory implements FactoryInterface
{
    public function createService(ServiceLocatorInterface $serviceLocator)
    {
        return new ApiJsonResponseService($serviceLocator);
    }
}
以下是我的可调用服务类:

class ApiJsonResponseService
{
    protected $serviceManager;

    protected $httpCode = null;

    protected $content = null;

    public function __construct(ServiceLocatorInterface $serviceManager)
    {
        $this->serviceManager = $serviceManager;
        return $this;
    }

    public function getServiceManager()
    {
        return $this->serviceManager;
    }

    public function setHttpCode($httpCode)
    {
        $this->httpCode = $httpCode;
        return $this;
    }

    public function getHttpCode()
    {
        return $this->httpCode;
    }

    public function setContent(ApiBaseResponseInterface $content)
    {
        $this->content = $content;
        return $this;
    }

    public function getContent()
    {
        return $this->content;
    }

    public function dispatch()
    {
        if (null === $this->httpCode) {
            throw new \Exception('Invalid or missing HTTP Code, set valid HTTP code using setHttpCode()');
        }
        if (null === $this->content) {
            throw new \Exception('Invalid or missing content');
        }
        $response = $this->getServiceManager()->get('Response');
        $response->setStatusCode($this->getHttpCode());
        $apiJsonResponse = new ApiJsonResponse($this->content);
        return $apiJsonResponse->dispatch();
    }
}

这正如预期的那样工作,我想知道是否有更好的方法可以做到这一点?

您不能将参数传递给服务管理器,但可以将参数传递给AbstractPluginManager。也就是说,我不会为此使用这种策略

我也不会将ServiceManager传递给您的
ApiJsonResponseService
类。服务管理器只获取响应对象,为什么不传递响应对象本身呢

您的第一个代码块变成:

$apiSuccess = new ApiSuccess(array(
    'status' => 'success',
    'code' => 'user_authenticate_success',
    'href' => 'app/welcome',
    'message' => 'User Authenticated Successfully'
));

$response = $this->getResponse();
$apiResponse = new ApiJsonResponseService($response, 200, $apiSuccess);
$apiResponse->dispatch();

显然,你还必须更新构造函数,响应的可用性取决于你在应用程序中的位置/如果你注入了它。

谢谢@Tim,我不喜欢的是让ApiJsonResponseService依赖于$response对象,想象一下,如果我出于某种原因需要请求对象,我可能会改变很多代码,相反,我会依赖服务管理器来提供我所需要的,+1让我知道我无法在服务管理器中传递参数。我明白你的意思,但是“如果”游戏可能会产生一些非常糟糕的界面。在这种情况下,您并没有真正注入依赖项。您强制此类了解服务管理器(以及如何检索请求对象),而它真正需要做的只是请求。