0

我通过 POST 表单将特定值发布到我的 CakePHP 应用程序。在 AppController 中我处理这个特定的 POST 数据,然后我取消设置 $this->request->data,例如

    if(!empty($this->request->data['Keyboard'])) {
        $this->Session->write('Keyboard.active', $this->request->data['Keyboard']['active']);
        unset($this->request->data);
    }

之后我希望所有请求数据都被取消设置(因此$this->request->data)。在我的子控制器中,我调用parent::beforefilter();其各自beforeFilter函数中的第一行代码,确保启动beforeFiltermy appController。但是,在我的 childController 中,以下代码行仍将评估为 true:

if($this-request->is('post'))

如何“取消” is('post') 调用?我也在上面尝试过$this->request->is('post') = falseif-statement但是没有成功。

4

1 回答 1

1

The "problem" with $this->request->is($anything) (in your case at least) is that it is just a function to compare variables, not a setter.

Here's the API code in case you want to check it out.

So, basically, this code compares the variable env('REQUEST_METHOD') to POST (that's in the detectors part of the code).

There's no way a $this->request->is('post') = false is going to work. Something like

$_ENV["REQUEST_METHOD"] = null;
//or
unset($_ENV["REQUEST_METHOD"]);

after you unset the request data might work, but I don't guarantee anything because I haven't tried it.

I feel this approach is very dirty, I don't like to mess with those variables at all if there's no function already available for it, but I expect you to have considered all possible choices to avoid messing with the request already and see this as the only solution. Otherwise you should post another question addressing this subject and see if better approaches come up.

Hopes it helps in your case.

于 2013-04-19T18:44:44.657 回答