0

我基于 Zend 的专辑示例创建了一个 Zend Framework 2 管理网站。一切都在作为我的默认控制器的 HomeController 中工作。但是,当我创建另一个控制器时,不会加载视图。这是我在 indexAction 中的代码。

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 }

但是,当我删除 line 时,视图会很好地加载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',
        ),
    ),

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

4

1 回答 1

3

我认为您已经使用了帮手 ViewModel ( Zend\View\Helper\ViewModel) 来为您提供这个

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

请使用模型 ViewModel ( Zend\View\Model\ViewModel) 而不是 helper,如下所示

use Zend\View\Model\ViewModel;

希望对你有帮助!

于 2013-11-05T07:02:31.670 回答