如何在zend框架中创建一个服务层?

时间:2013-06-29 03:40:48

标签: zend-framework2

我需要为Zend框架两个控制器功能创建一个服务层,以便将服务与控制器分离。

2 个答案:

答案 0 :(得分:4)

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

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

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

namespace ModuleName\Service;

class ExampleService
{
    public function SomeFunctionNameHere()
    {
        echo 'Hello World';
    }
}

现在编辑您的Module.php并将服务层添加到您的invokables,IE:

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;
}

这应该让你开始。

答案 1 :(得分:1)

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

应用/控制器/插件/ GetAccessLevel.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;
    }
}

然后将插件添加到配置中。

./模块/应用/配置/ module.config.php

'controller_plugins' => array(
    'invokables' => array(
        'getAccessLevel' => 'Application\Controller\Plugin\GetAccessLevel'
    )
),

现在每个控制器都可以访问此插件。

一些控制器

public function someAction() {
    $accessLevel = $this->getAccesslevel(array('User Role Entities Go Here'));
}