0

I'm posting a specific value through a POST form to my CakePHP app. Within the AppController I handle this specific POST data and afterwards I unset $this->request->data, e.g.

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

Afterwards I want ALL request data to be unset (hence the $this->request->data). Within my child controllers I call parent::beforefilter(); as the first line of code in its respective beforeFilter function, making sure the beforeFilter of my appController is initiated. However, in my childController the following line of code will still evaluate to true:

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

How do I 'unset' the is('post') call? I've also tried $this->request->is('post') = false in the if-statement above, with no success however.

2
  • Doesnt $this->request->is('post') check the "method" attribute in your form? As long as you have method = 'post', it'll always be true. Commented Apr 19, 2013 at 14:10
  • Yes, but I do actually need to POST data, but I want to handle it in the appcontroller in the specific instance it contains the index 'Keyboard'. After that the child controller should 'see' $this->request->is('post') as being false (a lot of already existing code is relying on that statement as a condition). Commented Apr 19, 2013 at 14:49

1 Answer 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.

Sign up to request clarification or add additional context in comments.

2 Comments

Thanks for your reply! Unfortunately, it doesn't work. Probably because some variable is set within CakePHP storing the $_ENV["REQUEST_METHOD"] value, which is then later used for evaluation. I've also tried unsetting $detect (yes really) but to no avail. However, I've come up with an 'elegant' solution, I've added $this->redirect($this->referer()); to the if-statement, which practically does the same. It refers to the same page, but without the POST data.
You're right, that solution does get the job done. You should post it as your accepted answer for anyone else with the same problem who doesn't bother to read the comment section :)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.