ZF2:如何在服务中调用视图助手?
ZF2 : How to call a view helper in a service?
我需要在此服务中使用视图助手 (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
中的未定义函数 Com\Service\Invokable\MyPaginationControl()
并且这个控制器被javascript (AJAX)调用。
当我评论该行时一切正常:
$tab .= myPaginationControl(...
我验证了我的视图助手 myPaginationControl 在 phtml 文件中使用时可以正常工作。
预先感谢您的帮助。
在您的控制器中执行此操作:
$this->getServiceLocator()->get('ViewHelperManager')->get("toto_display_service")
请注意,这不是最佳解决方案。相反,我建议您通过 __constructor(){}
将 viewhelper 传递给工厂
namespace Application\Factory\Controller;
use Application\Controller\MyClassNameController;
use Zend\Mvc\Controller\ControllerManager;
class MyCLassNameFactory
{
/**
* @{inheritDoc}
*/
public function __invoke(ControllerManager $controllerManager)
{
$serviceLocator = $controllerManager->getServiceLocator();
$controller = new MyClassNameController(
$serviceLocator->get('ViewHelperManager')->get('toto_display_service');
);
return $controller;
}
}
在 MyClassController 中
class MyClassNameController extends ADD_ANOTHER_CONTROLLER
{
private $todoService = null;
public function __construct($todoService = null)
{
$this->todoService = $todoService
}
}
然后在Module.php或module.config.php中添加配置。我更喜欢 module.config.php 文件。
'controllers' => [
'factories' => [
'Application\Controller\MyClassName' => 'Application\Factory\Controller\MyClassNameFactory'
],
],
不要忘记从可调用对象中删除控制器,否则它将无法工作并且您会发生冲突
编辑
$tab .= myPaginationControl
应该是 $tab .= new myPaginationControl
或 $tab .= $this->myPaginationControl
如果上述方法不起作用转储 $this->getServiceLocator()->get('ViewHelperManager')
的内容找到 myPaginationControl
并像那样调用它
$this->getServiceLocator()->get('ViewHelperManager')->get("myPaginationControl");
我需要在此服务中使用视图助手 (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
中的未定义函数 Com\Service\Invokable\MyPaginationControl()并且这个控制器被javascript (AJAX)调用。 当我评论该行时一切正常: $tab .= myPaginationControl(...
我验证了我的视图助手 myPaginationControl 在 phtml 文件中使用时可以正常工作。
预先感谢您的帮助。
在您的控制器中执行此操作:
$this->getServiceLocator()->get('ViewHelperManager')->get("toto_display_service")
请注意,这不是最佳解决方案。相反,我建议您通过 __constructor(){}
将 viewhelper 传递给工厂namespace Application\Factory\Controller;
use Application\Controller\MyClassNameController;
use Zend\Mvc\Controller\ControllerManager;
class MyCLassNameFactory
{
/**
* @{inheritDoc}
*/
public function __invoke(ControllerManager $controllerManager)
{
$serviceLocator = $controllerManager->getServiceLocator();
$controller = new MyClassNameController(
$serviceLocator->get('ViewHelperManager')->get('toto_display_service');
);
return $controller;
}
}
在 MyClassController 中
class MyClassNameController extends ADD_ANOTHER_CONTROLLER
{
private $todoService = null;
public function __construct($todoService = null)
{
$this->todoService = $todoService
}
}
然后在Module.php或module.config.php中添加配置。我更喜欢 module.config.php 文件。
'controllers' => [
'factories' => [
'Application\Controller\MyClassName' => 'Application\Factory\Controller\MyClassNameFactory'
],
],
不要忘记从可调用对象中删除控制器,否则它将无法工作并且您会发生冲突
编辑
$tab .= myPaginationControl
应该是 $tab .= new myPaginationControl
或 $tab .= $this->myPaginationControl
如果上述方法不起作用转储 $this->getServiceLocator()->get('ViewHelperManager')
的内容找到 myPaginationControl
并像那样调用它
$this->getServiceLocator()->get('ViewHelperManager')->get("myPaginationControl");