douwen5741 2013-11-05 04:13
浏览 58
已采纳

Zend Framework 2返回View Model无效

I created a Zend Framework 2 admin website based on Zend's Album example. Everything works in HomeController which is acted as my default controller. However when I created another controller, the view won't loaded. This is my code in 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;
    }
}

I'm sure that there is nothing wrong with the table data because it showed up all data when I printed it. However, the result of var_dump are always like this:

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

But, the view will loaded nicely when I remove the line return $vm;, this is useless of course, because I can't pass any data to view if I don't return the ViewModel.

In case it needed, this is my view manager setting in 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',
        ),
    ),

Please show me where is my mistake. Thank you.

  • 写回答

1条回答 默认 最新

  • doujumiao5024 2013-11-05 07:02
    关注

    I think you have used helper ViewModel ( Zend\View\Helper\ViewModel ) that gives you this

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

    Please use model ViewModel (Zend\View\Model\ViewModel) instead of helper like below

    use Zend\View\Model\ViewModel;
    

    Hope this will help you!

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 outlook无法配置成功
  • ¥30 这是哪个作者做的宝宝起名网站
  • ¥60 版本过低apk如何修改可以兼容新的安卓系统
  • ¥25 由IPR导致的DRIVER_POWER_STATE_FAILURE蓝屏
  • ¥50 有数据,怎么建立模型求影响全要素生产率的因素
  • ¥50 有数据,怎么用matlab求全要素生产率
  • ¥15 TI的insta-spin例程
  • ¥15 完成下列问题完成下列问题
  • ¥15 C#算法问题, 不知道怎么处理这个数据的转换
  • ¥15 YoloV5 第三方库的版本对照问题