我有两个搜索页面。一个是精简搜索表单,另一个是高级搜索。
我希望用户在精简页面上输入他们的搜索,并被重定向到包含高级搜索字段的结果页面。
我目前遇到的两个问题是:
- 如何重定向?
- 字段留空时的最佳做法是什么?
目前,我一直在回顾索引以获取前两个搜索字段
精简搜索
public function indexAction()
{
$dbAdapter = $this->getServiceLocator()->get('Zend'Db'Adapter'Adapter');
$form = new HomeSearchForm($dbAdapter);
$request = $this->getRequest();
if ($request->isPost()) {
$homeSearch = new HomeSearch();
$form->setInputFilter($homeSearch->getInputFilter());
$form->setData($request->getPost());
if ($form->isValid()) {
$homeSearch->exchangeArray($form->getData());
if($homeSearch->search_zip == null)
{
$homeSearch->search_zip = 'null';
}
if($homeSearch->industry_name == null)
{
$homeSearch->industry_name = 'null';
}
return $this->redirect()->toRoute('home/results',array(
'zip'=>$homeSearch->search_zip ,
'industry'=>$homeSearch->industry_name,
));
}
else {
echo "Error";
}
}
return array('form' => $form);
}
结果/高级搜索
public function resultsAction()
{
$search_zip = $this->params()->fromPost('zip');
$search_industry = $this->params()->fromRoute('industry');
$search_language = $this->params()->fromPost('language');
$search_gender = $this->params()->fromRoute('gender');
$search_name = $this->params()->fromRoute('name');
echo var_dump($search_zip);
echo var_dump($search_industry);
echo var_dump($search_gender);
echo var_dump($search_name);
$dbAdapter = $this->getServiceLocator()->get('Zend'Db'Adapter'Adapter');
$proSearch = new SearchQuery($dbAdapter);
$form = new SearchForm($dbAdapter);
$request = $this->getRequest();
if ($request->isPost()) {
echo 'i post';
$search = new MainSearch();
$form->setInputFilter($search->getInputFilter());
$form->setData($request->getPost());
if ($form->isValid()) {
$search->exchangeArray($form->getData());
if($search->search_zip == null)
{
$search->search_zip = 'null';
}
if($search->industry_name == null)
{
$search->industry_name = 'null';
}
if($search->language_name == null)
{
$search->language_name = 'null';
}
if($search->pro_gender == null)
{
$search->pro_gender = 'null';
}
return $this->redirect()->toRoute('home/results', array(
'action' => 'results',
'zip' => $search->search_zip,
'industry' => $search->industry_name,
'language' => $search->language_name,
'gender' => $search->pro_gender,
'name' => $search->pro_name,
));
}
}
return array(
'form' => $form,
'pros' => $proSearch->proSearch($search_zip,$search_industry,$search_language,$search_gender,,$search_name),
);
}
更新
我能够使用我在下面发布的方法进行搜索。 但是我仍然无法在 url 中发布参数(我能够使用此代码获取我想要的 url,但是我在应用过滤器并将字段绑定到表单时遇到问题$this->form->setAttributes(array('method' => 'GET'));
帖子重定向插件不是用来防止用户返回的吗? 阅读手册后,我不确定如何使用此插件? 我不确定为什么该段没有使用以下代码进行设置,我过去曾使用过这个。
'search_zip'=> $this->search_zip
模块配置
'results' => array(
'type' => 'segment',
'options' => array(
'route' => 'results[/:search_zip][/:search_industry][/:language][/:gender][/:designation][/:name]',
'defaults' => array(
'controller' => 'Application'Controller'Index',
'action' => 'results',
结果行动
public function resultsAction()
{
$dbAdapter = $this->getServiceLocator()->get('Zend'Db'Adapter'Adapter');
$proSearch = new SearchQuery($dbAdapter);
$request = $this->getRequest();
$form = new SearchForm($dbAdapter);
if ($request->isPost()) {
$search = new MainSearch();
$form->setInputFilter($search->getInputFilter());
$form->setData($request->getPost());
if ($form->isValid()) {
$search->exchangeArray($form->getData());
$search_zip = $search->search_zip;
$search_industry = $search->search_industry;
$search_language = $search->search_language;
$search_gender = $search->search_gender;
$search_designation = $search->search_designation;
$search_name = $search->search_name;
}
}
return array(
'form' => $form,
'pros' => $proSearch->proSearch($search_zip,$search_industry,$search_language,$search_gender,$search_designation,$search_name),
);
}
结果视图
//Search Index
$title = 'Search';
$this->headTitle($title);
?>
<h1><?php echo $this->escapeHtml($title); ?></h1>
<?php
$form->setAttribute('action', $this->url(
'home/results',
array(
'action' => 'results',
'search_zip'=> $this->search_zip
)
));
//$form->setAttributes(array('method' => 'GET'));
$form->prepare();
echo $this->form()->openTag($form);
echo $this->formRow($form->get('industry_name'));
echo $this->formRow($form->get('search_zip'));
echo '<br>';
echo $this->formRow($form->get('language_name'));
echo $this->formRow($form->get('pro_gender'));
echo '<br>';
echo $this->formRow($form->get('designation_name'));
echo $this->formRow($form->get('pro_name'));
echo $this->formSubmit($form->get('submit'));
?>
<h2>Results</h2>
<table class="table">
<tr>
<th>First Name</th>
<th>Last Name</th>
<th>Street Address</th>
<th>Office Number</th>
<th>City</th>
<th>State</th>
<th>Zip</th>
<th> </th>
</tr>
<?php foreach ($pros as $pro) : ?>
<tr>
<td><?php echo $this->escapeHtml($pro->pro_first);?></td>
<td><?php echo $this->escapeHtml($pro->pro_last);?></td>
<td><?php echo $this->escapeHtml($pro->pro_street_address);?></td>
<td><?php echo $this->escapeHtml($pro->pro_office_number);?></td>
<td><?php echo $this->escapeHtml($pro->pro_city);?></td>
<td><?php echo $this->escapeHtml($pro->pro_state);?></td>
<td><?php echo $this->escapeHtml($pro->pro_zip);?></td>
<?php endforeach; ?>
</table>
首先,
我不会重定向...
在您的搜索视图 phtml 中,您可以简单地执行以下操作:
<?php $this->form->setAttribute('action', $this->url('your/results/route'))->prepare();?>
然后,您可以从处理帖子的索引中删除所有代码。我不会使用两个表单类,只使用一个,只在页面上呈现您需要的项目!
echo $this->form()->openTag($form);
echo $this->formRow($form->get('search_zip'));
echo $this->formRow($form->get('industry_name'));
echo $this->formSubmit($form->get('submit'));
echo $this->form()->closeTag();
如果用户未在字段中输入任何内容,只需将其留空即可
****更新*****
如果您确实想要使用分段路由,则可以在路由中将这些部分设置为可选
'options' => array(
'route' => '[/:language]',
)
在没有看到您的路由的情况下很难向您展示,但这应该有效。
见 http://framework.zend.com/manual/2.0/en/modules/zend.mvc.routing.html#zend-mvc-router-http-segment
我已经重构了您提供的代码以显示我将如何处理这个问题。
仔细研究该方法;关键的变化是封装功能。
先决条件
- 这两种形式都应
POST
resultsAction()
- 您只需要一种搜索对象"类型"(此处称为
Search
) - 封装!这两种形式都提供结果和搜索,如果您在逻辑上分离功能,它将使您的代码更容易(阅读和维护)
没有测试,那是你的工作!
public function simpleSearchAction()
{
$form = $this->getSimpleSearchForm();
return new ViewModel(compact('form'));
}
public function advancedSearchAction()
{
$form = $this->getAdvancedSearch();
return new ViewModel(compact('form'));
}
public function resultsAction()
{
$request = $this->getRequest();
$form = $this->getAdvancedSearch();
$results = array();
if ($request->isPost()) {
$form->setData($request->getPost());
if ($form->isValid()) {
$search = new Search();
$search->exchangeArray($form->getData());
// Execute the search and return the results
$results = $this->search($search);
// Populate the form with the search data
$form->bind($search);
}
}
return new ViewModel(array(
'form' => $form,
'results' => $results,
));
}
protected function search(Search $search)
{
// do search here, although this shouldn't be
// in the controller, rather a service!
return $yourResults;
}
// This should be injected into the controller
protected function getAdapter();
// Forms should be using the FormElementManager, not
// being created using new
protected function getSimpleSearchForm();
// Same as above
protected function getAdvancedSearch();
要考虑
- 发布重定向插件(如果您想要带有搜索词的漂亮URL)
- 依赖注入!您错过了 ZF2 的好处,在服务工厂中创建
DbAdapter
和Forms
,并将它们注入控制器的构造函数中。 - 服务层 - 移出
search
代码将使其可重用,并将控制器逻辑与业务逻辑分开。