Zend Framework 2自定义ViewHelper,用于控制器中的特定操作

时间:2015-01-02 05:41:54

标签: php zend-framework2

是否可以在控制器内部的特定操作中包含viewhelper。

这就是事情,我显示了一个包含事务记录的表,但是在将它显示到html表之前,我必须从其他服务返回的记录中获得一些视图逻辑。此逻辑不适合放在服务端和视图中。所以我认为这是ViewHelper的用武之地。

从Web教程中,他们将viewhelper放在module.config.php中,并将其包含在Module.php的bootstrap部分中。现在,如果viewhelper在另一个控制器或动作中全局使用,例如" url"这样做是可以的。购买我的助手只是专门用于一个控制器内的这一动作。

以下是部分操作代码以及我当前如何在Controller

中集成viewhelper
<?php
function listAction() {
    $viewModel = new \Zend\View\Model\ViewModel();

    $service = $this->getServiceLocator();

    $transactionService = $service->get('TransactionService');
    $records = $transactionService->getTransaction();

    $helper = new \Transaction\View\Helper\TransactionViewHelper();
    $helper->setServiceLocator($service);
    $records = $helper->render($records);

    $viewModel->setVariable('records', $records);
    return $viewModel();
}
?>

它不优雅,它有效,但也许有更好的方式

viewhelper的结构是这样的,我需要服务器,因为我不知道如何获得&#34; url&#34; viewhelper在我的viewhelper中,我需要阅读一些配置。此外,永远不会在list.phtml

中调用帮助程序
<?php

 class TransactionViewHelper extends \Zend\View\Helper\AbstractHelper implements \Zend\ServiceManager\ServiceLocatorAwareInterface {
     public function setServiceLocator(\Zend\ServiceManager\ServiceLocatorInterface $serviceLocator)
    {
        $this->serviceLocator = $serviceLocator;
        return $this;
    }

    public function getServiceLocator()
    {
        return $this->serviceLocator;
    }

    public function __invoke()
    { 
        return $this;
    }

    public function render($records)
    {
        //Some logic here
        return $result;
    }
}
?>

1 个答案:

答案 0 :(得分:0)

好的找到了更好的解决方案

我把它放在module.config.php中作为view_helpers&gt; invokables,然后从ViewHelperManager调用它。这样就会自动注入servicelocator。

这是module.config.php

的附加设置
<?php
//...
'view_helpers' => array(
    'invokables' => array(
         'transaction' => 'MyModule\View\Helper\TransactionViewHelper'
    )
)
//...
?>

这是特定操作的控制器代码

<?php
//...
function listAction() {
    $viewModel = new \Zend\View\Model\ViewModel();

    $service = $this->getServiceLocator();

    $transactionService = $service->get('TransactionService');
    $records = $transactionService->getTransaction();

    $helper = new \Transaction\View\Helper\TransactionViewHelper();
    $helper->setServiceLocator($service);
    $records = $helper->render($records);

    //The action it's call by ajaxAction so I use this one to return to that function
    //The ajaxAction function will format it to json 
    $viewHelperManager = $service->get('ViewHelperManager');
    $transactionRecord = $viewHelperManager->get('transaction');
    $result = $transactionRecord->render($records);
    return $result

    //If not ajax than just pass this variable to viewmodel
    //Then do echo $this->transactionRecord->render($records) in list.phtml
    $viewModel = new \Zend\View\Model\ViewModel();
    $viewModel->setVariable('records', $records);
    return $viewModel();

}
//...
?>

viewhelper类变得更加简单和干净

<?php
class TransactionViewHelper extends \Zend\View\Helper\AbstractHelper {

    public function __invoke()
    { 
        return $this;
    }

    public function render($records)
    {
        //Some logic here
        return $result;
    }
}
?>

注意访问其他帮助器插件,例如'url',只需在viewhelper中调用它

<?php
$url = $this->view->plugin('url');
$url('path/to/url', array('id' => ''));
?>
相关问题