搜索表单类型get(CakePHP 3)

时间:2016-06-07 14:05:18

标签: php cakephp

我有一个简单的搜索表单,方法如下:

JpaExceptionTranslatorAspect

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

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

控制我的网址:/ test / search /:search_id。

问题是我的表单将请求发送到/ test / search?id =:search_id。 我需要做什么,形成发送到正确的URL:/ test / search /:search_id ??

感谢。

1 个答案:

答案 0 :(得分: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){
    //....
}