Zend framework2 ZF2-如何解决Zend Framework 2数据库连接问题
我已经开始学习ZF2,并一直在努力使一个基本的积垢工作。为了了解如何做到这一点,我遵循了一些教程:Zend framework2 ZF2-如何解决Zend Framework 2数据库连接问题,zend-framework2,Zend Framework2,我已经开始学习ZF2,并一直在努力使一个基本的积垢工作。为了了解如何做到这一点,我遵循了一些教程: 从书中,ZF2,通过示例学习 目前,我无法让任何教程的工作已经复制了他们一字不差。我一直在浏览404页。对我来说,这是一个路由问题,但我对此并不陌生,我确信有很多变量需要考虑。 所以我的问题是: 我在哪里可以找到一个连接到简单数据库并从word go开始工作的简单、可工作的CRUD模块?这将有助于消除模块存在的问题,然后我将能够查看其他潜在问题 使用ZF2进行故障排除时的最佳做法是什么?例如,如
// Create user
//$this->createUser($form->getData());
我被转到注册完整页面。这意味着问题与以下方面有关:
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);
$tableGateway = new \Zend\Db\TableGateway\TableGateway('user',
$dbAdapter, null, $resultSetPrototype);
$user = new User();
$user->exchangeArray($data);
$userTable = new UserTable($tableGateway);
$userTable->saveUser($user);
return true;
}
有没有办法解决上述问题
view/users/register/index.phtml
<section class="register">
<h2>Register</h2>
<?php if ($this->error): ?>
<p class="error">
There were one or more issues with your submission.
Please correct them as
indicated below.
</p>
<?php endif ?>
<?php
$form = $this->form;
$form->prepare();
$form->setAttribute('action', $this->url(NULL,
array('controller'=>'register', 'action' => 'process')));
$form->setAttribute('method', 'post');
echo $this->form()->openTag($form);
?>
<dl class="zend_form">
<dt><?php echo $this->formLabel($form->get('name')); ?></dt>
<dd><?php
echo $this->formElement($form->get('name'));
echo $this->formElementErrors($form->get('name'));
?></dd>
<dt><?php echo $this->formLabel($form->get('email')); ?></dt>
<dd><?php
echo $this->formElement($form->get('email'));
echo $this->formElementErrors($form->get('email'));
?></dd>
<dt><?php echo $this->formLabel($form->get('password'));
?></dt>
<dd><?php
echo $this->formElement($form->get('password'));
echo $this->formElementErrors($form->get('password'));
?></dd>
<dt><?php echo $this->formLabel($form->get('confirm_password')); ?></dt>
<dd><?php
echo $this->formElement($form->get('confirm_password'));
echo $this->formElementErrors($form->get('confirm_password'));
?></dd>
<dd><?php
echo $this->formElement($form->get('submit'));
echo $this->formElementErrors($form->get('submit'));
?></dd>
</dl>
<?php echo $this->form()->closeTag() ?>
登记
您的提交有一个或多个问题。
请更正为
如下所示。
src/Users/Form/RegisterForm.php
<?php
// filename : module/Users/src/Users/Form/RegisterForm.php
namespace Users\Form;
use Zend\Form\Form;
class RegisterForm extends Form
{
public function __construct($name = null)
{
parent::__construct('Register');
$this->setAttribute('method', 'post');
$this->setAttribute('enctype','multipart/form-data');
//Add the required fields using the add method
$this->add(array(
'name' => 'name',
'attributes' => array('type' => 'text',),
'options' => array(
'label' => 'Full Name',
),
));
//Additional validators and filters
$this->add(array(
'name' => 'email',
'options' => array('label' => 'Email',),
'attributes' => array('required' => 'required','type' => 'email'),
'filters' => array( array( 'name' => 'StringTrim' ), ),
'validators' => array(
array(
'name' => 'EmailAddress',
'options' => array(
'messages' => array(
\Zend\Validator\EmailAddress::INVALID_FORMAT => 'Email address format is invalid'
)
)
)
)
));
//Add a password field
$this->add(array(
'name' => 'password',
'attributes' => array('type' => 'password',),
'options' => array(
'label' => 'Password',
),
));
//Add a confirm password field
$this->add(array(
'name' => 'confirm_password',
'attributes' => array('type' => 'password',),
'options' => array(
'label' => 'Confirm password',
),
));
$this->add(array(
'name' => 'submit',
'attributes' => array('type' => 'submit', 'value' => 'Add'),
'options' => array(
'label' => 'Register',
),
));
}
}
至于对ZF2应用程序(我发现的)进行故障排除的最佳方法是使用XDebug(或类似工具)并能够逐步执行
除此之外,该模块在开发过程中可能非常有用
如果在视图管理器
配置中打开显示异常
,您应该能够看到问题的实际原因。好的,在谷歌搜索后,我发现了问题。基本上,数据库详细信息不会自动包括在内。为了包含它们并解决此问题,我在application.conofig.php和模块_listener_选项中添加了以下内容:
'config_glob_paths' => array(
__DIR__ . '/autoload/{,*.}{global,local}.php'
),
假设您是从ZF skeleton应用程序开始的,那么您会得到Zend Framework风格的404页面吗?如果是,则可能是路由问题。如果没有,这是一个普通的Apache404页面,请检查您是否启用了mod_rewrite,并且您的.htaccess文件正在使用中。嗨,Tim,谢谢您的输入。我已经更新了我的问题,加入了一些代码。只有当我尝试向数据库添加数据时,应用程序才会崩溃,如果我注释掉该部分,它会通过验证动作并转移到正确的页面。是的,根据application/view/404.phtml pageah,我确实得到了一个404错误页面,好的,调用的错误页面没有输出实际的错误消息。我更新了这个,现在看到的错误是:Zend\ServiceManager\ServiceManager::get无法获取或创建Zend\Db\Adapter\Adapter的实例
'config_glob_paths' => array(
__DIR__ . '/autoload/{,*.}{global,local}.php'
),