Reputation: 965
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.
Upvotes: 0
Views: 761
Reputation: 3329
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!
Upvotes: 3