Cakephp 3.6:从表单获取数据-奇怪的解决方法

时间:2018-10-30 14:55:26

标签: php forms cakephp get request

我想实现一个搜索表单,所以这是我到目前为止所做的:

index.ctp

    <?php
    $base_url = array('controller' => 'Customers', 'action' => 'index');
    echo $this->Form->create('Filter',array('url' => $base_url, 'class' => 'filter'));
    // Add a basic search 
    echo $this->Form->input('search', array('label' => false, 'placeholder' => "Name or surname..."));

    echo $this->Form->submit("Search");

    echo $this->Form->end();
    ?>

CustomersController.php

public function index()
    {

        $conditions = array();

        debug($this->request->getData()); //form field is not passed
        debug($this->request->getData('search')); //form field is not passed
        $conditions['OR'] = array(
            array('Customers.name LIKE' => '%' . $this->request->getData('search') . '%'),
            array('Customers.surname LIKE' => '%' . $this->request->getData('search') . '%')
        );

        $this->Customers->recursive = 0;
        $this->paginate = array(
            'limit' => 8,
            'conditions' => $conditions
        );
        $this->set('customers', $this->paginate()); 

    }

但是搜索字段值未传递到controller。怎么了?

编辑

CustomersController.php代码有问题,因为如果将$base_url中的index.ctp更改为另一个控制器,那么我可以正确获取数据

但此工作

index.ctp

<div class="Search">
        <?php
        // The base url is the url where we'll pass the filter parameters
        $base_url = array('controller' => 'ExpiringServices', 'action' => 'index');
        echo $this->Form->create('Filter',array('url' => $base_url, 'class' => 'filter'));
        // Add a basic search 
        echo $this->Form->control('search', array('label' => false, 'placeholder' => "Name or surname..."));

        echo $this->Form->submit("Search");

        echo $this->Form->end();
        ?>
    </div>

ExpiringServicesController.php

public function index()
{

    $conn = ConnectionManager::get('default');


    debug($this->request->getData('search'));
    //.....
}

NEW EDIT(WORKAROUND)

奇怪的是,如果我改变了:

$base_url = array('controller' => 'Customers', 'action' => 'index');

收件人

$base_url = array('controller' => 'Customers', 'action' => 'index',123);

有效!但是为什么呢?

0 个答案:

没有答案