Zend framework2 如何在zend framwork 2中创建服务层?

Zend framework2 如何在zend framwork 2中创建服务层?,zend-framework2,Zend Framework2,我需要为Zend framework两个控制器功能创建一个服务层,以便将服务与控制器解耦。您需要使用ServiceManager(SM)才能正常工作 这只是我如何做到这一点的一个例子: 在ModuleName/src/ModuleName/中,创建一个名为Service的文件夹,并创建ExampleService.php,示例: namespace ModuleName\Service; class ExampleService { public function SomeFuncti

我需要为Zend framework两个控制器功能创建一个服务层,以便将服务与控制器解耦。

您需要使用ServiceManager(SM)才能正常工作

这只是我如何做到这一点的一个例子:

在ModuleName/src/ModuleName/中,创建一个名为Service的文件夹,并创建ExampleService.php,示例:

namespace ModuleName\Service;

class ExampleService
{
    public function SomeFunctionNameHere()
    {
        echo 'Hello World';
    }
}
现在编辑Module.php并将服务层添加到可调用项中,即:

public function getServiceConfig()
{
    return array(
        'invokables' => array(
            'ModuleName\Service\ExampleService' => 'ModuleName\Service\ExampleService',
        ),
    );
}
现在编辑ModuleNameController.php

protected $service_example;

public function indexAction()
{
    $service = $this->getServiceExample()->SomeFunctionNameHere();
}

private function getServiceExample()
{
    if (!$this->service_example) {
        $this->service_example = $this->getServiceLocator()->get('ModuleName\Service\ExampleService');
    }

    return $this->service_example;
}

这应该让您开始了。

根据您从服务中寻找的功能,您可以创建自定义控制器插件。例如,我编写了一个自定义控制器插件来获取用户的访问级别

Application/Controller/Plugin/GetAccessLevel.php 然后将插件添加到配置中

./module/Application/config/module.config.php 现在每个控制器都可以访问这个插件

某控制器
然后创建您的服务,并通过服务定位器引用它们。这段代码非常糟糕,在任何级别上都不应该被认为是一个好的实践。插件应该使用工厂创建,必要的对象应该通过构造函数注入。清晰和干净,我喜欢它!
namespace Application\Controller\Plugin;

use Zend\Mvc\Controller\Plugin\AbstractPlugin;
use Zend\ServiceManager\ServiceLocatorInterface;
use Zend\ServiceManager\ServiceLocatorAwareInterface;

class GetAccessLevel extends AbstractPlugin implements ServiceLocatorAwareInterface
{

/**
 * Set the service locator.
 *
 * @param ServiceLocatorInterface $serviceLocator
 * @return GetAccessLevel
 */
public function setServiceLocator(ServiceLocatorInterface $serviceLocator)
{
    $this->serviceLocator = $serviceLocator;
    return $this;
}

/**
 * Get the service locator.
 *
 * @return \Zend\ServiceManager\ServiceLocatorInterface
 */
public function getServiceLocator()
{
    return $this->serviceLocator;
}

/**
 * Takes an array of role objects and returns access level
 *
 * @param array of MyModule\Entity\Role objects
 * @return int Access Level
 */
public function __invoke(array $roles)
{
        // Default access level
        $accesslevel = 0;

        // Get Service Locator for view helpers
        $controllerPluginManager = $this->getServiceLocator();

        // Get application service manager
        $serviceManager = $controllerPluginManager->getServiceLocator();

        // Get application config
        $config = $serviceManager->get('Config');

        // Get the role associated with full access from config
        $fullAccessRole = $config['appSettings']['full_access_role'];

        // Does user have the role for full access?
        foreach ($roles as $roleObject) {
            if($roleObject->getName() == $fullAccessRole) {
                $accesslevel = 1;
                break;
            }
        }

        // Return access level
        return $accesslevel;
    }
}
'controller_plugins' => array(
    'invokables' => array(
        'getAccessLevel' => 'Application\Controller\Plugin\GetAccessLevel'
    )
),
public function someAction() {
    $accessLevel = $this->getAccesslevel(array('User Role Entities Go Here'));
}