View 如何在Zend Framework 2中实现应用程序/网站范围的表单?
在我的ZF2应用程序中,我有多个模块,其中一个是“搜索”。它提供了一个搜索表单、搜索逻辑和在数据库中找到的课程列表: 表单类View 如何在Zend Framework 2中实现应用程序/网站范围的表单?,view,zend-framework2,zend-form,zend-form2,View,Zend Framework2,Zend Form,Zend Form2,在我的ZF2应用程序中,我有多个模块,其中一个是“搜索”。它提供了一个搜索表单、搜索逻辑和在数据库中找到的课程列表: 表单类 namespace Search\Form; ... class CourseSearchForm extends Form { private $cities; public function __construct(array $cities) { parent::__construct('courseSearch');
namespace Search\Form;
...
class CourseSearchForm extends Form {
private $cities;
public function __construct(array $cities) {
parent::__construct('courseSearch');
$this->setCities($cities);
$this->setAttribute('method', 'post');
$this->add(array(
'name' => 'keyword',
'attributes' => array(
'type' => 'text',
),
));
$this->add(array(
'name' => 'city',
'type' => 'Zend\Form\Element\Select',
'options' => array(
'label' => 'Stadt',
'value_options' => $this->cities
),
));
// ...
}
public function setCities(array $cities) {
$this->cities = $cities;
}
}
namespace Search\Controller;
...
class SearchController extends AbstractActionController {
protected $courseTable;
public function searchAction() {
return $this->redirect()->toRoute('search-courses');
}
public function searchCoursesAction() {
$form = $this->getServiceLocator()->get('Search\Form\CourseSearchForm');
$request = $this->getRequest();
if ($request->isPost()) {
$courseSearchInput = new CourseSearchInput();
$form->setInputFilter($courseSearchInput->getInputFilter());
$form->setData($request->getPost());
if ($form->isValid()) {
$courseSearchInput->exchangeArray($form->getData());
$courses = $this->getCourseTable()->findAllByCriteria($courseSearchInput);
} else {
$courses = null;
}
} else {
$courses = null;
}
return new ViewModel(array(
'form' => $form,
'courses' => $courses,
'cities' => $this->getServiceLocator()->get('Cache\Model\CityStorage')->getCities(),
));
}
/**
* Gets a CourseTable object.
* @return Search\Model\CourseTable
*/
function getCourseTable() {
if (!$this->courseTable) {
$serviceLocator = $this->getServiceLocator();
$this->courseTable = $serviceLocator->get('Search\Model\CourseTable');
}
return $this->courseTable;
}
}
控制器类
namespace Search\Form;
...
class CourseSearchForm extends Form {
private $cities;
public function __construct(array $cities) {
parent::__construct('courseSearch');
$this->setCities($cities);
$this->setAttribute('method', 'post');
$this->add(array(
'name' => 'keyword',
'attributes' => array(
'type' => 'text',
),
));
$this->add(array(
'name' => 'city',
'type' => 'Zend\Form\Element\Select',
'options' => array(
'label' => 'Stadt',
'value_options' => $this->cities
),
));
// ...
}
public function setCities(array $cities) {
$this->cities = $cities;
}
}
namespace Search\Controller;
...
class SearchController extends AbstractActionController {
protected $courseTable;
public function searchAction() {
return $this->redirect()->toRoute('search-courses');
}
public function searchCoursesAction() {
$form = $this->getServiceLocator()->get('Search\Form\CourseSearchForm');
$request = $this->getRequest();
if ($request->isPost()) {
$courseSearchInput = new CourseSearchInput();
$form->setInputFilter($courseSearchInput->getInputFilter());
$form->setData($request->getPost());
if ($form->isValid()) {
$courseSearchInput->exchangeArray($form->getData());
$courses = $this->getCourseTable()->findAllByCriteria($courseSearchInput);
} else {
$courses = null;
}
} else {
$courses = null;
}
return new ViewModel(array(
'form' => $form,
'courses' => $courses,
'cities' => $this->getServiceLocator()->get('Cache\Model\CityStorage')->getCities(),
));
}
/**
* Gets a CourseTable object.
* @return Search\Model\CourseTable
*/
function getCourseTable() {
if (!$this->courseTable) {
$serviceLocator = $this->getServiceLocator();
$this->courseTable = $serviceLocator->get('Search\Model\CourseTable');
}
return $this->courseTable;
}
}
/module/Search/view/Search/Search/Search-courses.phtml中的列表视图
...
<?php
$form = $this->form;
$form->setAttribute('action', $this->url('search-courses'));
$form->prepare();
echo $this->form()->openTag($form);
echo $this->formRow($form->get('keyword'));
echo $this->formRow($form->get('city'));
// ...
echo $this->formSubmit($form->get('submit'));
echo $this->form()->closeTag();
?>
<?php
if ($courses instanceof Zend\Db\ResultSet\ResultSetInterface) {
echo $this->render('search/search/list-courses.phtml', array(
'courses' => $courses
));
}
?>
。。。
现在,搜索表单应该显示在网站的每个页面上。如何实现这样的全局表单视图?是否有“标准”方法/最佳实践来做到这一点
Thx您只需创建一个ViewHelper
。将表单注入ViewHelper,然后渲染视图并返回它。ViewHelper本身看起来可能与ZfcUser提供的非常相似,可以通过某种方式显示它:
1.创建自己的视图辅助对象
为了激励你
2.使用插件转发
我更喜欢使用automatix,不是我干的,是社区干的。您的代码与我作为示例ViewHelper链接的内容没有什么区别,基本上只是另一个答案;)简单地做一些事情,比如回答,也许它会被认为比我的更好,你的答案也会被接受。ViewHelper的开销比forward()
要小得多。我忘了写的其他方法有:-部分视图-和布局变量部分仅呈现模板,它不知道表单-您不能在每个操作中对每个请求手动实例化表单。对于给定的用例,布局变量是非常错误的方法。forward()或ViewHelper是可供选择的选项,如上所述,由于开销较小,ViewHelper更适合使用