CakePHP 2.0 discovery request type behaves strangely

Take a look at this code:

if ($this->request->is('post')){ $this->request->data['Profile']['userId'] = $this->Auth->user('id'); if ($this->Profile->save($this->request->data)){ $this->Profile->setPermissions($this->Profile->id, $this->request->data['Permission']); $this->NFSSession->setSuccessMessage('Your profile has been updated.'); }else{ $this->NFSSession->setSuccessMessage('There was a problem updating your profile. Please try again.'); } }else{ echo 'Not a post request!!?!?!?!?!'; debug($this->request->data); } 

When I submit the form to the appropriate view for this action, it seems that $ this-> request-> is ('post') returns false. The other end of the if / else statement is executed. Here's a weird bit - there is POST data, and my debug call ($ this-> request-> data) spits out the data I expect!

The data is transferred here:

 Array ( [Profile] => Array ( [aboutMe] => Hey there ) [Permission] => Array ( [Profile] => Array ( [aboutMe] => 1 ) ) ) 

Now I could just change $ this-> request-> is ('post') to! empty ($ this-> request-> data), but this will not affect the problem.

And is there something wrong with my code? What's happening?

Thanks!

+4
source share
2 answers

Try using this:

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

http://cakephp.lighthouseapp.com/projects/42648/tickets/2353

+13
source

When you create a form in CakePHP, FormHelper :: create () will use the information in the $ this-> request-> data to determine if your form is a form or a form to add. If your primary model key is in your data, a hidden input field is created to override the HTTP method by default. This is because maybe you are updating something.

 <form id="RecipeEditForm" method="post" action="/recipes/edit/5"> <input type="hidden" name="_method" value="PUT" /> ... </form> 

To check if your form was submitted when creating or updating, you can pass the array to CakeRequest :: is () as follows:

 if($this->request->is(array('post','put')) { //... } 

Additional information: http://book.cakephp.org/2.0/en/core-libraries/helpers/form.html#creating-forms

+1
source

Source: https://habr.com/ru/post/1393162/


All Articles