首页 > 解决方案 > Cakephp 3.6:注意(8):未定义索引:搜索但所需功能有效

问题描述

我正在实现这样的搜索功能:

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->input("search", array('label' => false, 'placeholder' => "Name or surname..."));

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

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

ExpiringServicesController.php

$searchInput = $this->request->data['search']; //line 27

但是,虽然搜索按预期工作,但我收到此错误:

注意(8):未定义索引:搜索[APP/Controller\ExpiringServicesController.php,第27行]

如果我使用debug($searchInput),我可以看到它包含搜索输入文本。但是如果我使用if (isset($_FILES[$this->request->data['search']]))它,它就不会进入 if 语句,就像它没有设置一样。

我该如何解决这个问题?

标签: phpsearchcakephp

解决方案


您应该getData()改用:

$searchInput = $this->request->getData('search');

这将防止未定义的索引错误,并且直接来自文档:

任何不存在的键都将返回null

$foo = $this->request->getData('Value.that.does.not.exist');
// $foo == null

推荐阅读