PRASA7
PRASA7

Reputation: 66

How to create a service layer in zend framwork two?

I need to create a service layer for Zend framework two controller functions in order to decouple the services from controllers.

Upvotes: 2

Views: 878

Answers (2)

chandlermania
chandlermania

Reputation: 360

Depending on the functionality you are looking for from your service, you might be able to create a custom Controller Plugin. For example, here's a custom controller plugin I wrote to get a user's access level.

Application/Controller/Plugin/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;
    }
}

Then add the plugin to the configuration.

./module/Application/config/module.config.php

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

Now every controller will have access to this plugin.

Some Controller

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

Upvotes: 1

Diemuzi
Diemuzi

Reputation: 3527

You're going to need to use the ServiceManager (SM) in order to make this work properly.

This is just an example of how I have done it:

In your ModuleName/src/ModuleName/ create a folder named Service and create your ExampleService.php, Example:

namespace ModuleName\Service;

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

Now edit your Module.php and add the Service Layer to your invokables, IE:

public function getServiceConfig()
{
    return array(
        'invokables' => array(
            'ModuleName\Service\ExampleService' => 'ModuleName\Service\ExampleService',
        ),
    );
}

Now edit your 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;
}

This should get you started.

Upvotes: 4

Related Questions