布局中的ZF2小部件/视图 [英] ZF2 widgets/views in layout

查看:88
本文介绍了布局中的ZF2小部件/视图的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在一个网站上工作,在我的Zend Framework 2布局中,我需要一个类似于窗口小部件"的视图. 小部件应显示服务器的运行状态(已完成).

I am working on a website where I need a "widget" like view in my layout of Zend Framework 2. the widget should show the operational status of the server (this is done).

(如果这是错误的MVC样式,请纠正我) 我已经用

(Correct me if this is bad MVC style) I have build a controller with the

function viewStatusAction(){ 
   ... 
   return $viewModel(array($vars))
}

然后,我想使用viewHelper来获取操作的状态. 这就是我卡住的地方.我知道如何创建viewHelper,但不知道从哪里开始从控制器操作获取返回的视图. 那我该怎么办呢?

then i want to use a viewHelper to get the status of the action. This is where I'm stuck. I know how to create the viewHelper, but not where to start to get the returned view from the controller action. So how do i do this?

推荐答案

这就是我所做的.这也应该是正确的方法

Here is what i did. This should also be the right way to do it

在module.php

in module.php

public function getViewHelperConfig()
{
    return array(
        'factories' => array(
            'statusWidget' => function ($sm) {
                -- some service handling --
                $statusWidget = new statusWidget($service);
                return $statusWidget;
            }
        )
    );
}

然后我在OperationalStatus \ View \ Helper中创建了一个viewHelper

then i created a viewHelper in operationalStatus\View\Helper

<?php
namespace operationalStatus\View\Helper;

use Zend\View\Helper\AbstractHelper;

class statusWidget extends AbstractHelper
{

    public function __construct($service){
        $this->service = $service
    }

    public function __invoke()
    {
        -- collect data from the service --

        return $this->getView()->render('operational-status/widget/status', array('operation' => $status));
    }

}

这篇关于布局中的ZF2小部件/视图的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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