Zend Framewrok 2 返回 ViewModel 不起作用


Zend Framewrok 2 return ViewModel not working

我根据Zend的相册示例创建了一个Zend Framework 2管理网站。一切都在家庭控制器中工作,它充当我的默认控制器。但是,当我创建另一个控制器时,视图不会加载。这是我在索引操作中的代码。

namespace Admin'Controller;
use Zend'Mvc'Controller'AbstractActionController;
use Zend'View'Helper'ViewModel;
use Admin'Model'Map'User;
class UserController extends AbstractActionController 
{
    protected $userTable;
    public function indexAction()
    {
        $users = $this->getUserTable()->fetchAll();
        $viewData = array(
            'users' => $users
        );
        $vm = new ViewModel($viewData);
        var_dump($vm);
        return $vm;
    }
    public function getUserTable()
    {
        if(!$this->userTable)
        {
            $sm = $this->getServiceLocator();
            $this->userTable = $sm->get('Admin'Model'Table'UserTable');
        }
        return $this->userTable;
    }
}

我确定表数据没有任何问题,因为它在我打印时显示了所有数据。但是,var_dump的结果总是这样的:

object(Zend'View'Helper'ViewModel)#280 (3) { ["current":protected]=> NULL ["root":protected]=> NULL ["view":protected]=> NULL }

但是,当我删除行return $vm;时,视图将很好地加载,这当然是无用的,因为如果我不返回 ViewModel,我将无法传递任何数据进行查看。

如果需要,这是我在 module.config 中的视图管理器设置.php:

// View
    'view_manager' => array(
        'display_not_found_reason' => true,
        'display_exceptions'       => true,
        'doctype'                  => 'HTML5',
        'not_found_template'       => 'error/404',
        'exception_template'       => 'error/index',
        'template_map' => array(
            'layout/layout'           => __DIR__ . '/../view/layout/layout.phtml',
            'error/404'               => __DIR__ . '/../view/error/404.phtml',
            'error/index'             => __DIR__ . '/../view/error/index.phtml',
            // Admin page index
            'admin/home/index'        => __DIR__ . '/../view/admin/home/index.phtml',
            'admin/user/index'        => __DIR__ . '/../view/admin/user/index.phtml',
            'admin/ticket/index'      => __DIR__ . '/../view/admin/ticket/index.phtml',
            'admin/transaction/index' => __DIR__ . '/../view/admin/transaction/index.phtml',
            'admin/customer/index'    => __DIR__ . '/../view/admin/customer/index.phtml',
            'admin/payment/index'     => __DIR__ . '/../view/admin/payment/index.phtml',
            'admin/comment/index'     => __DIR__ . '/../view/admin/comment/index.phtml',
            'admin/appstat/index'     => __DIR__ . '/../view/admin/appstat/index.phtml',
            'admin/sysstat/index'     => __DIR__ . '/../view/admin/sysstat/index.phtml',
            'admin/account/index'     => __DIR__ . '/../view/admin/account/index.phtml',
        ),
        'template_path_stack' => array(
            'home'        => __DIR__ . '/../view',
            'user'        => __DIR__ . '/../view',
            'ticket'      => __DIR__ . '/../view',
            'transaction' => __DIR__ . '/../view',
            'customer'    => __DIR__ . '/../view',
            'payment'     => __DIR__ . '/../view',
            'comment'     => __DIR__ . '/../view',
            'appstat'     => __DIR__ . '/../view',
            'sysstat'     => __DIR__ . '/../view',
            'account'     => __DIR__ . '/../view',
        ),
    ),

请告诉我我的错误在哪里。谢谢。

我认为您已经使用了辅助程序ViewModel(Zend'View'Helper'ViewModel)为您提供了这个

object(Zend'View'Helper'ViewModel)#280 (3) { ["current":protected]=> NULL ["root":protected]=> NULL ["view":protected]=> NULL }

请使用模型视图模型(Zend'View'Model'ViewModel)而不是如下所示的帮助程序

use Zend'View'Model'ViewModel;

希望这对您有所帮助!