Zend Framework comm-app找不到类

时间:2013-12-03 14:59:45

标签: php validation zend-framework zend-framework2

我正在关注this本书,我在第3章,此代码返回给我以下错误:致命错误:在/ var / www /中找不到类'Users \ Model \ User'第70行的CommunicationApp / module / Users / src / Users / Controller / RegisterController.php

我想知道代码有什么问题,或者代码是不是代码呢?

<?php
namespace Users\Controller;

use Zend\Mvc\Controller\AbstractActionController;
use Zend\View\Model\ViewModel;

use Users\Form\RegisterForm;
use Users\Form\RegisterFilter;

use Users\Model\User;
use Users\Model\UserTable;

class RegisterController extends AbstractActionController
{

public function indexAction()
{
    $form = new RegisterForm();
    $viewModel  = new ViewModel(array('form' => $form)); 
    return $viewModel; 
}

public function processAction()
{
    if (!$this->request->isPost()) {
        return $this->redirect()->toRoute(NULL , array( 
                    'controller' => 'register', 
                    'action' =>  'index' 
                ));
    }

    $post = $this->request->getPost();

    $form = new RegisterForm();
    $inputFilter = new RegisterFilter();
    $form->setInputFilter($inputFilter);

    $form->setData($post);
    if (!$form->isValid()) {
        $model = new ViewModel(array(
            'error' => true,
            'form'  => $form,
        ));
        $model->setTemplate('users/register/index');
        return $model;
    }

    // Create user
    $this->createUser($form->getData());

    return $this->redirect()->toRoute(NULL , array( 
                    'controller' => 'register', 
                    'action' =>  'confirm' 
                ));
}

public function confirmAction()
{
    $viewModel  = new ViewModel(); 
    return $viewModel; 
}

protected function createUser(array $data)
{

    $sm = $this->getServiceLocator();
    $dbAdapter = $sm->get('Zend\Db\Adapter\Adapter');

    $resultSetPrototype = new \Zend\Db\ResultSet\ResultSet();
    $resultSetPrototype->setArrayObjectPrototype (new \Users\Model\User); //line 70
    $tableGateway = new \Zend\Db\TableGateway\TableGateway('user' /* table name  */, $dbAdapter, null, $resultSetPrototype);

    $user = new User();
    $user->exchangeArray($data);

    $userTable = new UserTable($tableGateway);
    $userTable->saveUser($user);

    return true;
 }
}

1 个答案:

答案 0 :(得分:1)

如果模型文件位于以下目录中,则进行正确的自动加载检查 /module/Users/src/Users/Model/User.php

如果不是zend无法自动加载您的模型文件,并且如果(因为您指出模型文件中的类名和命名空间是正确的),则会触发您的错误。文件不会放在那里。