RSM*_*RSM 0 php oop model-view-controller zend-framework zend-form
我正在使用zend框架,并尝试使用zend表单,MVC和OOP输出一个简单的登录表单.
我的代码如下:Controller IndexController.php
class IndexController extends Zend_Controller_Action
{
public function init()
{
/* Initialize action controller here */
}
public function indexAction()
{
$this->view->loginForm = $this->getLoginForm();
}
public function getLoginForm()
{
$form = new Application_Form_Login;
return $form;
}
}
Run Code Online (Sandbox Code Playgroud)
这是以下形式:Login.php
class Application_Form_Login extends Zend_Form
{
public function init()
{
$form = new Zend_Form;
$username = new Zend_Form_Element_Text('username');
$username
->setLabel('Username')
->setRequired(true)
;
$password = new Zend_Form_Element_Password('password');
$password
->setLabel('Password')
->setRequired(true)
;
$submit = new Zend_Form_Element_Submit('submit');
$submit->setLabel('Login');
$form->addElements(array($username, $password, $submit));
}
}
Run Code Online (Sandbox Code Playgroud)
并且视图:index.phtml
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
</head>
<body>
<div id="header">
<div id="logo">
<img src="../application/images/logo.png" alt="logo">
</div>
</div>
<div id="wrapper">
<?php echo $this->loginForm; ?>
</div>
</body>
</html>
Run Code Online (Sandbox Code Playgroud)
我是Zend Framework,MVC和OOP的新手,所以这是我在以下在线建议,教程等方面的最佳尝试.
您无意中创建了一个没有元素的表单,这就是为什么没有出现的原因.在表单对象的init方法中,您正在创建一个新实例Zend_Form
,$form
然后不执行任何操作,而不是将元素添加到当前实例.将您的班级更改为:
class Application_Form_Login extends Zend_Form
{
public function init()
{
$username = new Zend_Form_Element_Text('username');
$username
->setLabel('Username')
->setRequired(true)
;
$password = new Zend_Form_Element_Password('password');
$password
->setLabel('Password')
->setRequired(true)
;
$submit = new Zend_Form_Element_Submit('submit');
$submit->setLabel('Login');
$this->addElements(array($username, $password, $submit));
}
}
Run Code Online (Sandbox Code Playgroud)
它应该工作.