Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/jsp/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Zend framework2 用域对象填充控制器的zf2工厂_Zend Framework2 - Fatal编程技术网

Zend framework2 用域对象填充控制器的zf2工厂

Zend framework2 用域对象填充控制器的zf2工厂,zend-framework2,Zend Framework2,我有两种不同的方法来加载控制器的域模型。我想听听哪个更好 第一种方法-传统。 控制器工厂将所需的服务注入控制器构造函数。在控制器操作中,根据请求参数加载模型: ClientAppointsControllerFactory.php class ClientAppointmentsControllerFactory implements FactoryInterface { public function createService(ServiceLocatorInterface $ser

我有两种不同的方法来加载控制器的域模型。我想听听哪个更好

第一种方法-传统。 控制器工厂将所需的服务注入控制器构造函数。在控制器操作中,根据请求参数加载模型:

ClientAppointsControllerFactory.php

class ClientAppointmentsControllerFactory implements FactoryInterface
{
    public function createService(ServiceLocatorInterface $serviceLocator) {    
        $serviceManager = $serviceLocator->getServiceLocator();
        $controller = new ClientAppointmentsController($serviceManager->get('Service\ClientAppointments'));
        return $controller;
    }
}
class ClientAppointmentsControllerFactory implements FactoryInterface
{
    public function createService(ServiceLocatorInterface $serviceLocator) {
        $getRequestParam = function($param) use($serviceLocator){
            $serviceManager = $serviceLocator->getServiceLocator();
            $request = $serviceManager->get('Request');
            $router  = $serviceManager->get('Router');
            $match   = $router->match($request); // \Zend\Mvc\Router\RouteMatch
            $result =  $match->getParam($param);
            return $result;
        };

        $serviceManager = $serviceLocator->getServiceLocator();
        $clientService = $serviceManager->get('Service\ClientAppointments');
        $appointments = $clientService->fetchByClientId($getRequestParam('clientId));
        $controller = new ClientAppointmentsController($appointments);
        return $controller;
    }
}
ClientAppointsController.php

class ClientAppointmentsController extends AbstractActionController
{
    public function __construct(AppointmentFactory $appointmentFactory){
        $this->appointmentFactory = $appointmentFactory;
    }

    public function indexAction() {
        $viewModel = $this->acceptableViewModelSelector($this->acceptCriteria);
        $appointments = $this->appointmentFactory->getClientAppointments($this->params()->fromRoute('clientId'));
        $viewModel->setVariables([
            'appointments' => $appointments
        ]);
        return $viewModel;
    }
}
class ClientAppointmentsController extends AbstractActionController
{
    /**
     * @param Array $appointments Array of Appointment objects
     */
    public function __construct(Array $appointments){
        $this->appointments = $appointments
    }

    public function indexAction() {
        $viewModel = $this->acceptableViewModelSelector($this->acceptCriteria);
        $viewModel->setVariables([
            'appointments' => $this->appointments
        ]);
        return $viewModel;
    }
第二种方法-访问工厂中的请求/路由参数 这对我来说似乎有点干净,因为现在控制器对服务层没有依赖性,只希望(从任何源)加载的对象数组传递给视图。我认为这仍然符合工厂的定义,因为它正在创建具有所需依赖项的控制器,尽管现在正在积极创建它们,而不是将其传递给控制器来执行以下操作:

ClientAppointsControllerFactory.php

class ClientAppointmentsControllerFactory implements FactoryInterface
{
    public function createService(ServiceLocatorInterface $serviceLocator) {    
        $serviceManager = $serviceLocator->getServiceLocator();
        $controller = new ClientAppointmentsController($serviceManager->get('Service\ClientAppointments'));
        return $controller;
    }
}
class ClientAppointmentsControllerFactory implements FactoryInterface
{
    public function createService(ServiceLocatorInterface $serviceLocator) {
        $getRequestParam = function($param) use($serviceLocator){
            $serviceManager = $serviceLocator->getServiceLocator();
            $request = $serviceManager->get('Request');
            $router  = $serviceManager->get('Router');
            $match   = $router->match($request); // \Zend\Mvc\Router\RouteMatch
            $result =  $match->getParam($param);
            return $result;
        };

        $serviceManager = $serviceLocator->getServiceLocator();
        $clientService = $serviceManager->get('Service\ClientAppointments');
        $appointments = $clientService->fetchByClientId($getRequestParam('clientId));
        $controller = new ClientAppointmentsController($appointments);
        return $controller;
    }
}
ClientAppointsController.php

class ClientAppointmentsController extends AbstractActionController
{
    public function __construct(AppointmentFactory $appointmentFactory){
        $this->appointmentFactory = $appointmentFactory;
    }

    public function indexAction() {
        $viewModel = $this->acceptableViewModelSelector($this->acceptCriteria);
        $appointments = $this->appointmentFactory->getClientAppointments($this->params()->fromRoute('clientId'));
        $viewModel->setVariables([
            'appointments' => $appointments
        ]);
        return $viewModel;
    }
}
class ClientAppointmentsController extends AbstractActionController
{
    /**
     * @param Array $appointments Array of Appointment objects
     */
    public function __construct(Array $appointments){
        $this->appointments = $appointments
    }

    public function indexAction() {
        $viewModel = $this->acceptableViewModelSelector($this->acceptCriteria);
        $viewModel->setVariables([
            'appointments' => $this->appointments
        ]);
        return $viewModel;
    }
哪个更好


(我还想到了一个浮动的可变工厂。)

在我看来,第二个根本不好,因为它混合了创建逻辑和业务逻辑。这意味着业务逻辑错误将阻止工厂工作

第一个更好,但不太好,因为现在控制器中有了业务逻辑

我建议将业务逻辑移动到业务模型或控制器插件中