2016-06-07 48 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