CAKEPHP 4-设置请求数据
我们有一个Cakephp 3.x应用程序,已更新为最新的Cakephp 4.x。作为这项工作的一部分,我们也从PHP 7更改为PHP 8。
在测试应用程序的同时,我们注意到一项功能已停止工作。
该应用程序是一个可搜索的数据库,并与 redis 用于缓存。其中一个功能意味着在页面重新加载之间保留用户搜索。这可以通过将序列化的形式数据编写到Redis中,然后将其重新填充到模板中的Input
字段中。这意味着用户看到他们输入的搜索标准;当页面刷新时,他们不需要重新输入搜索标准。
cakephp 3.x应用中的代码重新填充input
表单字段看起来像这样:
$form_fields = ['f1', 'f2', 'f3'];
$ form_fields
array包含form input
input
/代码>在模板中。例如:
<input type="text" name="f1">
代码的下一部分重新启动表单。在这种情况下,$ user_search
是已从redis获得且未重新审判的数据数组。例如,我们可能有$ user_search ['f1']
和$ user_search ['f3']
包含redis data; f2
没有填充,因为用户没有使用该字段搜索。
foreach ($form_fields as $form_field) {
$this->request->getData()[$form_field] = (isset($user_search[$form_field])) ? $user_search[$form_field] : '';
}
在蛋糕3.x应用程序中,以上功能正常。重新加载页面后,由于设置 请求数据,例如在上面的循环中,它评估为:
$this->request->getData()['f1'] = 'foo';
$this->request->getData()['f3'] = 'bar';
这意味着请求数据具有“ foo”为f1 和“ bar”为
f3
。 f2
中没有任何内容,因此根据:''';
条件将其设置为一个空字符串。
在cakephp 4.x应用中,这确实不工作;所有表单字段均未在页面重新加载中供电。我已经确认他们没有通过修改:'';
条件上的上面提到的:'test';
并确保字符串“ test”来确认它们被设置为空字符串。没有在田野中显示。
数据存在于Redis中,我已经确认$ user_search
包含上面显示的内容 - 换句话说,数据并不缺少,因此我们已经排除了这一点。
当我阅读 https://book.cake.cake.cake.orgp.org/4/ en/controllers/request-response.html 我看不到设置请求数据的示例。有一个方法getData()
可以执行您的期望 - 读取请求数据。
有没有办法在蛋糕4.x中设置请求数据以使上述代码可以工作?
在Vanilla php中,我们正在做的事情等同于
$_POST['f1'] = 'foo';
$_POST['f2'] = ''; // empty string as no value set by user
$_POST['f3'] = 'bar';
Afaik,这在PHP中是有效的;您可以在代码中使用任何内容设置/覆盖请求数据。如果这是错误的,请建议我应该做什么。
为了清楚起见,我们以这种方式设置请求数据是因为通过AJAX调用搜索作品。当用户最初输入其搜索标准时,该页面已重新加载,因此表单字段似乎正确填充了。此问题发生在页面重新加载上。在这种情况下,我们希望用它们在重新加载页面之前输入的值重新填充表单。
We have a CakePHP 3.x app which we've updated to the latest CakePHP 4.x. As part of this work we've also changed from PHP 7 to PHP 8.
Whilst testing the app we noticed a feature that had stopped working.
The app is a searchable database and is integrated with Redis for caching. One of the features means that the users search is retained between page reloads. This works by writing serialized form data to Redis, and then re-populating that back into the input
fields in the template. This means the user sees the search criteria they entered; they do not need to re-enter their search criteria when the page is refreshed.
The code in the CakePHP 3.x app which re-populated the input
form fields looked like this:
$form_fields = ['f1', 'f2', 'f3'];
The $form_fields
array contains the names of the form input
's in the template. As an example:
<input type="text" name="f1">
The next part of the code re-populates the form. In this case $user_search
is an array of data that has been obtained and unserialized from Redis. As an example we might have $user_search['f1']
and $user_search['f3']
containing Redis data; f2
is unpopulated because the user didn't search using that field.
foreach ($form_fields as $form_field) {
$this->request->getData()[$form_field] = (isset($user_search[$form_field])) ? $user_search[$form_field] : '';
}
In the Cake 3.x app the above works fine. When the page is reloaded the form fields are set due to setting the request data, e.g. in the loop above, it evalulates to:
$this->request->getData()['f1'] = 'foo';
$this->request->getData()['f3'] = 'bar';
This means the request data has "foo" as f1
and "bar" as f3
. There is nothing in f2
so it gets set to an empty string as per the : '';
condition.
In the CakePHP 4.x app this does not work; all form fields are unpopulated on page reload. I've confirmed that they are not being set to empty strings by modifying the : '';
condition mentioned above to : 'test';
and ensured the string "test" is not being shown in the fields.
The data exists in Redis and I've confirmed that $user_search
contains what's shown above - in other words the data is not missing so we've ruled that out.
When I read over https://book.cakephp.org/4/en/controllers/request-response.html I can't see an example of setting request data. There is a method getData()
which does what you'd expect - it reads the request data.
Is there a way to set the request data in Cake 4.x such that the above code would work?
In vanilla PHP what we're doing is equivalent to
$_POST['f1'] = 'foo';
$_POST['f2'] = ''; // empty string as no value set by user
$_POST['f3'] = 'bar';
AFAIK this was - and still is - valid in PHP; you can set/overwrite request data with anything in your code. If this is wrong please advise what I should be doing instead.
For clarity the reason we are setting request data in this manner is because the search for works via an ajax call. When the user enters their search criteria initially, the page has not been reloaded so the form fields appear to be populated correctly. This issue occurs on page reload. In that instance we want to repopulate the form with the values they entered prior to the page being reloaded.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您要查找的功能是
withData
。请记住,请求对象是不可变的,因此您需要将该函数的结果分配给请求对象,例如使用$ this-&gt; setRequest($ this-&this-&gt; getRequest() - &gt; wertdata( 'f1','foo') - &gt; withData('f3','bar'))
。The function you're looking for is
withData
. Remember that the request object is immutable, so you need to assign the result of that function call back into the request object, e.g. with$this->setRequest($this->getRequest()->withData('f1', 'foo')->withData('f3', 'bar'))
.