2

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

4

2 回答 2

4

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

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

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

namespace ModuleName\Service;

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

现在编辑您的 Module.php 并将服务层添加到您的可调用文件 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;
}

这应该让你开始。

于 2013-06-30T18:54:06.723 回答
1

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

应用程序/控制器/插件/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/Application/config/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'));
}
于 2013-07-01T21:13:12.513 回答