ZF2:如何在服务中调用视图助手?

时间:2015-08-15 18:47:24

标签: javascript ajax service zend-framework2 viewhelper

我需要在此服务中使用视图助手( myPaginationControl ):

/.../
class TotoDisplayService implements ServiceLocatorAwareInterface
{
    public function getReponse ($paginator)
    {
        $tab = '<table>';
        /.../

        $tab .= myPaginationControl($paginator, 'sliding','pagination_control_file',array('route' => 'paginator'));

        /.../
        foreach ($paginator as $t) {
            //something to list : THIS WORKS
        }
        $tab .= '</table>';

        return array(
            'result' => $tab
        );
    }
/.../

此服务由此控制器调用:

public function displayAction()
{
    $em = $this->getEntityManager();
        // query to database
    $q1 = $this->serviceLocator->get('toto_list_service');
    $rep1 = $q1->getReponse($em);

    $paginator = new PaginatorZF(new DoctrinePaginator(new PaginatorORM($rep1)));
    $paginator->setCurrentPageNumber ($this->params()->fromRoute('page', 1))->setItemCountPerPage(25);

        // prepares something to display with javascript
    $q2 = $this->serviceLocator->get('toto_display_service');
    $rep2 = $q2->getReponse($paginator);

        // return to javascript 
    return new JsonModel(
        $rep2
    );
}

这是MyPaginationControl(与paginationControl相同,我做了测试)     

use Zend\View\Helper\AbstractHelper;

class MyPaginationControl extends AbstractHelper
{
    public function __invoke($a, $b, $c, $d)
    {
        $PaginationControl = $this->getView()->plugin('paginationcontrol');

        return $PaginationControl($a,$b,$c,$d);
    }
}

这是module.config.php

    'service_manager' => array(
        'invokables' => array (
            'toto_list_service' => 'Com\Service\Invokable\TotoListService',
            'toto_display_service' => 'Com\Service\Invokable\TotoDisplayService'
        )
    ),
    'view_helpers' => array(
        'invokables' => array(
            'myPaginationControl' => 'Com\View\Helper\MyPaginationControl',
            ),
    ),

这是module.php(没什么特别的)

class Module implements AutoloaderProviderInterface
{
    public function getAutoloaderConfig()
    {
        return array(
            'Zend\Loader\ClassMapAutoloader' => array(
                __DIR__ . '/autoload_classmap.php',
            ),
            'Zend\Loader\StandardAutoloader' => array(
                'namespaces' => array(
                   __NAMESPACE__ => __DIR__ . '/src/' . tr_replace('\\', '/' , __NAMESPACE__),
                ),
            ),
        );
    }

    public function getConfig()
    {
        return include __DIR__ . '/config/module.config.php';
    }

    public function onBootstrap(MvcEvent $e)
    {
        $eventManager        = $e->getApplication()->getEventManager();
        $moduleRouteListener = new ModuleRouteListener();
        $moduleRouteListener->attach($eventManager);
    }
}

这里出现错误信息:     在D:\ Zend \ Apache2 \ htdocs \ Gestion \ module \ Com \ src \ Com \ Service \ Invokable \ TotoDisplayService.php

此控制器由javascript(AJAX)调用。 当我评论这条线时,一切正常: $ tab。= myPaginationControl(...

我验证了我的视图助手myPaginationControl在我在phtml文件中使用时完美无缺。

提前感谢您的帮助。

0 个答案:

没有答案