[英]How do I pass variables to another action from a search form?
我有兩個搜索頁面。 一種是簡明搜索形式,另一種是高級搜索。
我希望用戶在壓縮頁面上輸入搜索,然后重定向到包含高級搜索字段的結果頁面。
我目前遇到的兩個問題是:
目前,我一直在回顧索引以獲取前兩個搜索字段
壓縮搜索
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'));
Post Redirect插件不是用來阻止用戶返回的嗎? 閱讀手冊后,我不確定如何使用此插件? 我不確定為什么該段未使用以下代碼進行設置,我過去曾使用過此代碼。
'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();
想一下
DbAdapter
和Forms
在服務工廠並把它們注入到控制器的構造函數。 search
代碼將使其可重用,並將控制器邏輯與業務邏輯分開。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.