【发布时间】:2014-09-07 02:44:21
【问题描述】:
我正在尝试将我的登录表单放在位于 layout.phtml 中的标题中
我在 Application/Module.php 中创建了我的表单对象
<?php
namespace Application;
use Zend\Mvc\ModuleRouteListener;
use Zend\Mvc\MvcEvent;
use Users\Form\LoginForm;
class Module
{
public function onBootstrap(MvcEvent $e)
{
.
.
.
$form = new LoginForm();
$form->get('submit')->setValue('Login');
$application = $e->getParam('application');
$viewModel = $application->getMvcEvent()->getViewModel();
$viewModel->form = $form;
}
.
.
.
}
这是为了使表单可用于每个页面(因为 layout.phtml 用于呈现每个页面)
对于我的 layout.phtml 文件:
<?php echo $this->doctype(); ?>
<?php
$form = $this->layout()->form;
//\Zend\Debug\Debug::dump($form);
$form->prepare();
$form->setAttribute('action', $this->url(NULL, array('controller' => 'Users', 'action' => 'login')));
?>
<html>
<head>
<?php echo $this->headTitle('Budget') ?>
<?php echo $this->headMeta()
->appendName('viewport', 'width=device-width, initial-scale=1, maximum-scale=1')
->appendHttpEquiv('X-UA-Compatible', 'IE=edge') ?>
<?php echo $this->headLink(array('rel' => 'shortcut icon', 'type' => 'image/vnd.microsoft.icon', 'href' => $this->basePath() . '/img/favicon.ico'))
->prependStylesheet($this->basePath() . '/css/style.css')
->prependStylesheet($this->basePath() . '/css/bootstrap-theme.min.css')
->prependStylesheet($this->basePath() . '/css/bootstrap.min.css') ?>
</head>
<body>
<div class="header">
<nav class="navbar navbar-default" role="navigation">
<div class="container-fluid">
<!-- Brand and toggle get grouped for better mobile display -->
<div class="navbar-header">
<button type="button" class="navbar-toggle" data-toggle="collapse" data-target="#bs-example-navbar-collapse-1">
<span class="sr-only">Toggle navigation</span>
<span class="icon-bar"></span>
<span class="icon-bar"></span>
<span class="icon-bar"></span>
</button>
<a href="/liftshare/" class="navbar-brand">Lift share</a></div>
<!-- Collect the nav links, forms, and other content for toggling -->
<div class="collapse navbar-collapse" id="bs-example-navbar-collapse-1">
<ul class="nav navbar-nav">
<li><a href="/liftshare/search">Search</a></li>
<li><a href="/liftshare/offers">Offer a lift</a></li>
</ul>
<ul class="nav navbar-nav navbar-right">
<?php echo $this->form()
->setAttribute('class', 'navbar-form')
->setAttribute('role', 'form')
->openTag($form); ?>
<div class="form-group">
<?php echo $this->formLabel($form->get('email')
->setLabelAttributes(array('id' => 'login_email'))
->setLabelAttributes(array('class' => 'sr-only'))
->setLabelAttributes(array('placeholder' => 'Enter email'))
); ?>
<input class="form-control" id="exampleInputEmail2" placeholder="Enter email" type="email">
</div>
<div class="form-group">
<label class="sr-only" for="exampleInputPassword2">Password</label>
<input class="form-control" id="exampleInputPassword2" placeholder="Password" type="password">
</div>
<button type="submit" class="btn btn-default">Sign in</button>
<?php echo $this->form()->closeTag(); ?>
</ul></div><!-- /.navbar-collapse -->
</div>
</nav>
</div>
<?php echo $this->content; ?>
<?php echo $this->inlineScript() ?>
<!-- Scripts -->
<?php echo $this->headScript()
->prependFile($this->basePath() . '/js/bootstrap.min.js')
->prependFile($this->basePath() . '/js/jquery.min.js')
->prependFile($this->basePath() . '/js/respond.min.js', 'text/javascript', array('conditional' => 'lt IE 9',))
->prependFile($this->basePath() . '/js/html5shiv.js','text/javascript', array('conditional' => 'lt IE 9',))
; ?>
</body>
</html>
我收到了错误 - Fatal error: Call to undefined method Zend\Form\View\Helper\Form::setAttribute() in /var/www/budget_zend/module/Application/view/layout/layout.phtml on line 55
我了解在我的其他视图中可用的表单助手可能在布局视图中不可用。但是我如何在 layout.phtml 文件中放置一个表单呢?另外,我必须以不同的方式定义它似乎有点奇怪 - 例如,我在某些时候想要将表单从标题(在 layout.phtml 文件中)移动到主要内容区域 - 这意味着我需要在后端更改内容以进行前端更新。似乎不对。有人可以告诉我我应该怎么做。
谢谢
【问题讨论】:
标签: php zend-framework zend-framework2