View Helper中的Zend Framework 2服务 [英] Zend Framework 2 Service in View Helper

查看:87
本文介绍了View Helper中的Zend Framework 2服务的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要编写一个获取服务并对其进行处理的视图助手.我成功实现了视图帮助程序,可以访问服务定位器.问题在于,调用__invoke方法时,无法通过服务定位器找到我要获取的服务.

I need to write a view helper that gets a service and do something with it. I successfully implemented the view helper to have access to the service locator. The problem is that the service I want to get is not being found through the service locator when the __invoke method is called.

视图助手代码:

<?php

namespace Application\View\Helper;

use Zend\View\Helper\AbstractHelper,
    Zend\ServiceManager\ServiceLocatorAwareInterface,

    Application\Model;

class LoggedCustomer extends AbstractHelper implements ServiceLocatorAwareInterface
{

    use \Zend\ServiceManager\ServiceLocatorAwareTrait;

    public function __invoke()
    {

        $model = new Model\Customer($this->getServiceLocator());

        return $model->getCurrent();

    }

}

模型代码的片段:

namespace Application\Model;

use Application\Entity,
    Andreatta\Model\Base as Base;

class Customer extends Base
{

    /**
     * 
     * @return Zend\Authentication\AuthenticationService
     */
    public function getAuthService()
    {

        $serviceLocator = $this->getServiceLocator();

        return $serviceLocator->get('Application\Auth');

    }

    /**
     * 
     * @return Zend\Authentication\Adapter\AdapterInterface
     */
    protected function getAuthAdapter()
    {

        return $this->getAuthService()->getAdapter();

    }

    public function getCurrent()
    {

        $authService = $this->getAuthService();

        if ($authService->hasIdentity())
            return $authService->getIdentity();

        return null;

    }

来自module.config.php的代码段:

The snippet from module.config.php:

'service_manager' => array
(

    'factories' => array
    (

        'Application\Auth' => function($sm)
        {

            $authService = $sm->get('doctrine.authenticationservice.application');
            $authService->setStorage( new \Zend\Authentication\Storage\Session('Application\Auth'));

            return $authService;

        },

    ),

),

'view_helpers' => array
(

    'invokables' => array
    (

        'loggedCustomer' => 'Application\View\Helper\LoggedCustomer',

    ),

),

从任何视图调用视图助手时,都会得到以下信息:

When calling the view helper from any view I get the following:

Zend\View\HelperPluginManager::get was unable to fetch or create an instance for Application\Auth

奇怪的是,应用程序运行正常(即该服务通常被应用程序的其他部分使用).

The weird is that the application is functioning correctly (i.e. this service is being normally used by other parts of the application).

我做了一些研究,我认为可以通过视图帮助器中的服务管理器访问的唯一服务是在module.config.php的"view_manager"部分中注册的服务.有谁知道如何访问其他服务?

I did some research and I think the only services that I can access through the service manager inside the view helper are the ones registered inside the 'view_manager' section of module.config.php. Does anyone have an idea of how to access the other services?

推荐答案

$this->getServiceLocator()在视图助手中只能获取您需要使用$this->getServiceLocator()->getServiceLocator()来获取应用程序服务的其他视图助手

$this->getServiceLocator() in view helper can only get u other view helpers you need to use $this->getServiceLocator()->getServiceLocator() to get the application services

这篇关于View Helper中的Zend Framework 2服务的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆