今天我发现了一个完全奇怪的行为,我想停止它。
我使用CakePHP 2.9并具有搜索表格,我想通过该表格进行搜索。我的主要密钥是由于与其他模型的关系而导致的session_id。
class Order extends AppModel
{
public $primaryKey = 'session_id';
我用
打开搜索表格 <?php echo $this->Form->create(
'Order',
array(
'url' => array('controller' => 'orders', 'action' => 'search')
)
); ?>
导致以下HTML:
<form id="OrderSearchForm" action="/orders/search" method="post" accept-charset="utf-8">
在控制器中,我会根据是否的要求进行不同的操作。为了调试怪异的行为,我在控制器中添加了以下调试线:
if ($this->request->is('post')) {
debug('post');
} else if ($this->request->is('get')) {
debug('get');
} else {
debug($_REQUEST);
debug($_GET);
debug($_POST);
}
,只要我搜索订单的session_id以外的其他值或将session_id与其他搜索值组合在一起,一切都很好。但是,一旦我仅搜索session_id,搜索表的生成的html就会更改为
<form id="OrderSearchForm" action="/orders/search/mysearchvaluesessionidstring" method="post" accept-charset="utf-8">
然后,当我提交我的表格时,突然不再将其视为"帖子"。$ _GET的调试输出仍然为空,并且仍然填充了$ _ post的调试输出,但是如果($ this-> request-> is('post'(不再触发条件。
如何改变此行为?
当请求数据仅包含会话ID时,蛋糕可能会将请求类型更改为PUT
。您可以通过检查if ($this->request->is(array('post', 'put')))
而不是仅检查POST
请求来解决此问题。
您可以阅读更多有关为什么在这里发生这种情况的更多信息