php - 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 语句,就像它没有设置一样。
我该如何解决这个问题?
解决方案
您应该getData()
改用:
$searchInput = $this->request->getData('search');
这将防止未定义的索引错误,并且直接来自文档:
任何不存在的键都将返回
null
$foo = $this->request->getData('Value.that.does.not.exist'); // $foo == null
推荐阅读
- django - django 中的购物车会话
- excel - HTTPRequest.SetClientCertificate
- mysql - MySQL:在输出为 CSV 格式之前在查询结果的标题上方添加文本
- logging - 如何在 Fastlane 日志中隐藏/屏蔽密码?
- angular - 角度使用 src/assets 处理大量图像,可以吗
- flutter - 退出后停止收听提供商
- ms-access - 将十进制数添加到 MS Access 查询
- css - 标题未居中对齐
- python - 将描述作为属性添加到 Pandas DataFrame 列
- c# - SQL 语句中的 Seats Academy 计算