2016-06-07 51 views
0

我有方法的簡單的搜索得到這樣的:搜索表單式的get(CakePHP的3)

<?= $this->Form->create('Search',['type'=>'get']) ?> 
<fieldset> 
    <legend><?= __('Search') ?></legend> 
    <?php 
     echo $this->Form->input('id'); 
    ?> 
</fieldset> 
<?= $this->Form->button(__('Submit')) ?> 
<?= $this->Form->end() ?> 

在我的routes.php文件我有路由器控制:

$routes->connect(
    '/test/search/:id', 
    array('controller' => 'test', 'action' => 'search'), // Local à ser redirecionado 
    array(
     'pass' => array('id'), 
     'id' => '[0-9]+' 
)); 

爲了控制我的網址如:/ test/search /:search_id。

問題是我的表單發送請求到/ test/search?id =:search_id。 我需要做什麼,以形成發送到正確的網址:/ test/search /:search_id?

謝謝。

回答

1

這可以在你的控制器使用簡單的方法

首先使用POST方法

<?= $this->Form->create('Search',['type'=>'post']) ?> 
<fieldset> 
    <legend><?= __('Search') ?></legend> 
    <?php 
     echo $this->Form->input('id'); 
    ?> 
</fieldset> 
<?= $this->Form->button(__('Submit')) ?> 
<?= $this->Form->end() ?> 

來解決

use Cake\Event\Event; 

public function beforeFilter(Event $event){ 
    parent::beforeFilter($event); 
     if($this->request->is('post') && isset($this->request->data['id']){ 
      return $this->redirect([ 
       'action' => 'search', 
       $this->request->data['id'] 
      ]); 
     } 

    } 

public function search($id = null){ 
    //.... 
} 
使用
+0

'你controller' beforeFilter影響該控制器中的所有操作 - 最好將該檢查放入搜索功能中。還要注意,這可以用js解決(onsubmit - > document.location = x; return false)。 – AD7six